Skip to content

Commit 7176286

Browse files
author
唐敦旺
committed
init: jsonp
1 parent 606ed75 commit 7176286

10 files changed

+408
-1
lines changed

.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
types
13+
dist-ssr
14+
*.local
15+
16+
# Editor directories and files
17+
.vscode/*
18+
!.vscode/extensions.json
19+
.idea
20+
.DS_Store
21+
*.suo
22+
*.ntvs*
23+
*.njsproj
24+
*.sln
25+
*.sw?

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# jsonp
1+
## 使用

favicon.svg

+15
Loading

index.html

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="favicon.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite App</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module">
12+
import jsonp from './src/main.ts'
13+
const data = await jsonp('/api/index.php?r=cms/cms-search/update-unread&jsonp={callback}')
14+
console.log('🚀 ~ file: index.html ~ line 14 ~ data', data)
15+
</script>
16+
</body>
17+
</html>

package.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "@tangdw/jsonp",
3+
"version": "0.0.1",
4+
"author": "tangdw<[email protected]>",
5+
"license": "MIT",
6+
"repository": "https://github.com/tangdw/jsonp",
7+
"main": "./dist/jsonp.umd.js",
8+
"module": "./dist/jsonp.es.js",
9+
"types": "types/jsonp.d.ts",
10+
"exports": {
11+
".": {
12+
"import": "./dist/jsonp.es.js",
13+
"require": "./dist/jsonp.umd.js"
14+
}
15+
},
16+
"files": [
17+
"dist",
18+
"types"
19+
],
20+
"keywords": [
21+
"jsonp",
22+
"promise"
23+
],
24+
"scripts": {
25+
"dev": "vite",
26+
"build": "tsc && vite build",
27+
"preview": "vite preview"
28+
},
29+
"devDependencies": {
30+
"typescript": "^4.5.4",
31+
"vite": "^2.8.0"
32+
}
33+
}

src/main.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const TIMEOUT = 10000
2+
3+
let g = window as any,
4+
d = g.document,
5+
id = 0,
6+
el = (type: string) => d.createElement(type)
7+
8+
export default (url: string) => {
9+
return new Promise((resolve, reject) => {
10+
let fn = `callback_${++id}`,
11+
b = d.body || el('body'),
12+
s = el('script'),
13+
timer: number,
14+
err: string
15+
16+
g[fn] = (data: any) => {
17+
clearTimeout(timer)
18+
err ? reject(err) : resolve(data)
19+
b.removeChild(s)
20+
s = g[fn] = null
21+
delete g[fn]
22+
}
23+
24+
timer = setTimeout(() => {
25+
err = 'Timed out'
26+
g[fn]()
27+
}, TIMEOUT)
28+
29+
s.src = url.replace(/{callback}/i, fn)
30+
b.appendChild(s)
31+
})
32+
}

src/vite-env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

tsconfig.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"lib": ["ESNext", "DOM"],
5+
"types": ["vite/client"],
6+
"baseUrl": "./",
7+
"declaration": true,
8+
"emitDeclarationOnly": true,
9+
"target": "ESNext",
10+
"useDefineForClassFields": true,
11+
"moduleResolution": "Node",
12+
"strict": true,
13+
"sourceMap": true,
14+
"outDir": "./types",
15+
"resolveJsonModule": true,
16+
"esModuleInterop": true,
17+
"noUnusedLocals": true,
18+
"noUnusedParameters": true,
19+
"noImplicitReturns": true
20+
},
21+
"include": ["src"]
22+
}

vite.config.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineConfig } from 'vite'
2+
3+
export default defineConfig({
4+
build: {
5+
lib: {
6+
name: 'jsonp',
7+
entry: 'src/main.ts',
8+
},
9+
},
10+
server: {
11+
proxy: {
12+
'/api/index.php': {
13+
target: 'https://xxxx',
14+
secure: false,
15+
changeOrigin: true,
16+
cookieDomainRewrite: '',
17+
headers: {
18+
cookie: 'xx登录态xx'
19+
},
20+
},
21+
},
22+
},
23+
})

0 commit comments

Comments
 (0)