cantian-request 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/api.d.ts +11 -0
- package/dist/api.js +35 -0
- package/dist/api.js.map +1 -0
- package/dist/basic.d.ts +3 -0
- package/dist/basic.js +49 -0
- package/dist/basic.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/sse.d.ts +2 -0
- package/dist/sse.js +54 -0
- package/dist/sse.js.map +1 -0
- package/package.json +17 -0
package/README.md
ADDED
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type API_OPTIONS = {
|
|
2
|
+
basepath?: string;
|
|
3
|
+
path: string;
|
|
4
|
+
key?: string;
|
|
5
|
+
method: 'GET' | 'POST' | 'PATCH';
|
|
6
|
+
data?: Object;
|
|
7
|
+
personateSub?: string;
|
|
8
|
+
source?: string;
|
|
9
|
+
locale?: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const api: (options: API_OPTIONS) => Promise<any>;
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { getTraceId } from 'cantian-als';
|
|
2
|
+
import { createThrowHttpErrorFetch, createWithConcurrencyFetch, createWithTimeoutFetch } from './basic.js';
|
|
3
|
+
const basicApi = createWithConcurrencyFetch(10, createThrowHttpErrorFetch(createWithTimeoutFetch(10000, fetch)));
|
|
4
|
+
export const api = async (options) => {
|
|
5
|
+
const { basepath = process.env.API_BASEPATH, path, key = process.env.API_KEY_INTERNAL, data, method } = options;
|
|
6
|
+
const headers = {
|
|
7
|
+
'Content-Type': 'application/json',
|
|
8
|
+
Authorization: `Bearer ${key}`,
|
|
9
|
+
'x-trace-id': getTraceId(),
|
|
10
|
+
};
|
|
11
|
+
if (options.personateSub) {
|
|
12
|
+
headers['x-personate-sub'] = options.personateSub;
|
|
13
|
+
}
|
|
14
|
+
if (options.source) {
|
|
15
|
+
headers['x-source'] = options.source;
|
|
16
|
+
}
|
|
17
|
+
if (options.locale) {
|
|
18
|
+
headers['x-locale'] = options.locale;
|
|
19
|
+
}
|
|
20
|
+
const body = data ? JSON.stringify(data) : undefined;
|
|
21
|
+
const url = `${basepath}${path}`;
|
|
22
|
+
const init = { headers, body, method };
|
|
23
|
+
const response = await basicApi(url, init);
|
|
24
|
+
if (response.status !== 204) {
|
|
25
|
+
const text = await response.text();
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(text);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
console.log(`FETCH_ERROR_API_INVALID_JSON`, { url, init, text });
|
|
31
|
+
throw new Error(`API returns a non-JSON string: ${text}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=api.js.map
|
package/dist/api.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AAa3G,MAAM,QAAQ,GAAG,0BAA0B,CAAC,EAAE,EAAE,yBAAyB,CAAC,sBAAsB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC;AAEjH,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,EAAE,OAAoB,EAAE,EAAE;IAChD,MAAM,EAAE,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAChH,MAAM,OAAO,GAAG;QACd,cAAc,EAAE,kBAAkB;QAClC,aAAa,EAAE,UAAU,GAAG,E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urrency: number, originalFetch?: typeof fetch) => (input: string | URL | Request, init?: RequestInit | undefined) => Promise<Response>;
|
|
2
|
+
export declare const createWithTimeoutFetch: (timeout: number, originalFetch?: typeof fetch) => typeof fetch;
|
|
3
|
+
export declare const createThrowHttpErrorFetch: (originalFetch?: typeof fetch) => typeof fetch;
|
package/dist/basic.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { limitFunction } from 'p-limit';
|
|
2
|
+
// 注意只重试非http错误,避免本机网络抖动造成的问题
|
|
3
|
+
const basicFetch = async (input, init) => {
|
|
4
|
+
let lastError;
|
|
5
|
+
for (let i = 0; i < 2; i++) {
|
|
6
|
+
if (i !== 0) {
|
|
7
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
8
|
+
}
|
|
9
|
+
const startedAt = new Date().toISOString();
|
|
10
|
+
try {
|
|
11
|
+
return await fetch(input, init);
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (init?.signal?.aborted) {
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
lastError = error;
|
|
18
|
+
console.warn(`FETCH_ERROR_${i + 1}_TIMES`, { input, init, startedAt, now: new Date().toISOString() }, error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
console.error(`FETCH_ERROR_LAST_TIME`, { input, init }, lastError);
|
|
22
|
+
throw lastError;
|
|
23
|
+
};
|
|
24
|
+
export const createWithConcurrencyFetch = (concurrency, originalFetch = basicFetch) => {
|
|
25
|
+
return limitFunction(originalFetch, { concurrency });
|
|
26
|
+
};
|
|
27
|
+
export const createWithTimeoutFetch = (timeout, originalFetch = basicFetch) => {
|
|
28
|
+
const withTimeoutFetch = (input, init) => {
|
|
29
|
+
const timeoutSignal = AbortSignal.timeout(timeout);
|
|
30
|
+
return originalFetch(input, {
|
|
31
|
+
...init,
|
|
32
|
+
signal: init?.signal ? AbortSignal.any([init.signal, timeoutSignal]) : timeoutSignal,
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
return withTimeoutFetch;
|
|
36
|
+
};
|
|
37
|
+
export const createThrowHttpErrorFetch = (originalFetch = basicFetch) => {
|
|
38
|
+
const throwHttpErrorFetch = async (input, init) => {
|
|
39
|
+
const response = await originalFetch(input, init);
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
const text = await response.text();
|
|
42
|
+
console.error(`FETCH_ERROR_HTTP_NOT_OK`, { input, init, text });
|
|
43
|
+
throw new Error(`Fetch failed(${response.status}): ${text}`);
|
|
44
|
+
}
|
|
45
|
+
return response;
|
|
46
|
+
};
|
|
47
|
+
return throwHttpErrorFetch;
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=basic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"basic.js","sourceRoot":"","sources":["../src/basic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,6BAA6B;AAC7B,MAAM,UAAU,GAAiB,KAAK,EAAE,KAAK,EAAE,IAAK,EAAE,EAAE;IACtD,IAAI,SAAc,CAAC;IACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC3B,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;YACZ,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;QAChD,CAAC;QACD,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QAC3C,IAAI,CAAC;YACH,OAAO,MAAM,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;gBAC1B,MAAM,KAAK,CAAC;YACd,CAAC;YACD,SAAS,GAAG,KA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AAC,CAAC;QAClD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;YACjB,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;YAChE,MAAM,IAAI,KAAK,CAAC,gBAAgB,QAAQ,CAAC,MAAM,MAAM,IAAI,EAAE,CAAC,CAAC;QAC/D,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IACF,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/sse.d.ts
ADDED
package/dist/sse.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createParser } from 'eventsource-parser';
|
|
2
|
+
import { createThrowHttpErrorFetch, createWithConcurrencyFetch } from './basic.js';
|
|
3
|
+
const basicSse = createThrowHttpErrorFetch(createWithConcurrencyFetch(10));
|
|
4
|
+
export const sse = async function* (input, init) {
|
|
5
|
+
const response = await basicSse(input, init);
|
|
6
|
+
if (!response.body) {
|
|
7
|
+
console.error(`FETCH_ERROR_NO_BODY`);
|
|
8
|
+
throw new Error('No body');
|
|
9
|
+
}
|
|
10
|
+
const reader = response.body.getReader();
|
|
11
|
+
const pending = [];
|
|
12
|
+
let parserError;
|
|
13
|
+
const decoder = new TextDecoder();
|
|
14
|
+
const parser = createParser({
|
|
15
|
+
onEvent(event) {
|
|
16
|
+
pending.push(event);
|
|
17
|
+
},
|
|
18
|
+
onError(error) {
|
|
19
|
+
parserError = error;
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
let error;
|
|
23
|
+
try {
|
|
24
|
+
while (true) {
|
|
25
|
+
while (pending.length) {
|
|
26
|
+
yield pending.shift();
|
|
27
|
+
}
|
|
28
|
+
let chunk;
|
|
29
|
+
chunk = await reader.read();
|
|
30
|
+
const { value, done } = chunk;
|
|
31
|
+
if (done) {
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
parser.feed(decoder.decode(value, { stream: true }));
|
|
35
|
+
if (parserError) {
|
|
36
|
+
throw parserError;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
error = e;
|
|
42
|
+
try {
|
|
43
|
+
await reader.cancel();
|
|
44
|
+
}
|
|
45
|
+
catch { }
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
if (!error) {
|
|
50
|
+
reader.releaseLock();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=sse.js.map
|
package/dist/sse.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.js","sourceRoot":"","sources":["../src/sse.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAsB,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,yBAAyB,EAAE,0BAA0B,EAAE,MAAM,YAAY,CAAC;AAEnF,MAAM,QAAQ,GAAG,yBAAyB,CAAC,0BAA0B,CAAC,EAAE,CAAC,CAAC,CAAC;AAC3E,MAAM,CAAC,MAAM,GAAG,GAAG,KAAK,SAAS,CAAC,EAAE,KAAwC,EAAE,IAAkB;IAC9F,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC7C,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;QACnB,OAAO,CAAC,KAAK,CAAC,qBAAqB,CAAC,C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EAAE,CAAC;YACX,MAAM,CAAC,WAAW,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;AACH,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "cantian-request",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./dist/index.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"prepublishOnly": "rm -rf dist && tsc"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"cantian-als": "^0.0.24"
|
|
16
|
+
}
|
|
17
|
+
}
|