@scalar/snippetz 0.2.4 → 0.2.6
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 -3
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/types.d.ts +30 -5
- package/dist/core/types.d.ts.map +1 -1
- package/dist/core/types.test-d.d.ts +2 -0
- package/dist/core/types.test-d.d.ts.map +1 -0
- package/dist/core/utils/arrayToObject.d.ts +6 -1
- package/dist/core/utils/arrayToObject.d.ts.map +1 -1
- package/dist/core/utils/arrayToObject.js +6 -1
- package/dist/core/utils/needsQuotes.d.ts +7 -0
- package/dist/core/utils/needsQuotes.d.ts.map +1 -0
- package/dist/core/utils/needsQuotes.js +10 -0
- package/dist/core/utils/objectToString.d.ts +5 -0
- package/dist/core/utils/objectToString.d.ts.map +1 -1
- package/dist/core/utils/objectToString.js +7 -2
- package/dist/plugins/js/fetch/fetch.d.ts +5 -2
- package/dist/plugins/js/fetch/fetch.d.ts.map +1 -1
- package/dist/plugins/js/fetch/fetch.js +63 -59
- package/dist/plugins/js/fetch/index.js +1 -1
- package/dist/plugins/js/ofetch/index.js +1 -1
- package/dist/plugins/js/ofetch/ofetch.d.ts +5 -2
- package/dist/plugins/js/ofetch/ofetch.d.ts.map +1 -1
- package/dist/plugins/js/ofetch/ofetch.js +70 -64
- package/dist/plugins/node/fetch/fetch.d.ts +5 -2
- package/dist/plugins/node/fetch/fetch.d.ts.map +1 -1
- package/dist/plugins/node/fetch/fetch.js +63 -59
- package/dist/plugins/node/fetch/index.js +1 -1
- package/dist/plugins/node/ofetch/index.js +1 -1
- package/dist/plugins/node/ofetch/ofetch.d.ts +5 -2
- package/dist/plugins/node/ofetch/ofetch.d.ts.map +1 -1
- package/dist/plugins/node/ofetch/ofetch.js +70 -64
- package/dist/plugins/node/undici/index.js +1 -1
- package/dist/plugins/node/undici/undici.d.ts +5 -2
- package/dist/plugins/node/undici/undici.d.ts.map +1 -1
- package/dist/plugins/node/undici/undici.js +63 -59
- package/dist/plugins/shell/curl/curl.d.ts +6 -0
- package/dist/plugins/shell/curl/curl.d.ts.map +1 -0
- package/dist/plugins/shell/curl/curl.js +105 -0
- package/dist/plugins/shell/curl/index.d.ts +2 -0
- package/dist/plugins/shell/curl/index.d.ts.map +1 -0
- package/dist/plugins/shell/curl/index.js +1 -0
- package/dist/snippetz.d.ts +9 -12
- package/dist/snippetz.d.ts.map +1 -1
- package/dist/snippetz.js +22 -22
- package/package.json +7 -2
- package/dist/core/utils/isKeyNeedsQuotes.d.ts +0 -2
- package/dist/core/utils/isKeyNeedsQuotes.d.ts.map +0 -1
- package/dist/core/utils/isKeyNeedsQuotes.js +0 -5
package/README.md
CHANGED
|
@@ -69,13 +69,13 @@ const snippet = snippetz().hasPlugin('node', 'undici')
|
|
|
69
69
|
You can also just use one specific plugin to keep your bundle size small.
|
|
70
70
|
|
|
71
71
|
```ts
|
|
72
|
-
import {
|
|
72
|
+
import { nodeUndici } from '@scalar/snippetz/plugins/node/undici'
|
|
73
73
|
|
|
74
|
-
const
|
|
74
|
+
const result = nodeUndici.generate({
|
|
75
75
|
url: 'https://example.com',
|
|
76
76
|
})
|
|
77
77
|
|
|
78
|
-
console.log(source
|
|
78
|
+
console.log(source)
|
|
79
79
|
|
|
80
80
|
// import { request } from 'undici'
|
|
81
81
|
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,qBAAqB,CAAA;AACnC,cAAc,wBAAwB,CAAA;AAEtC,cAAc,SAAS,CAAA"}
|
package/dist/core/index.js
CHANGED
package/dist/core/types.d.ts
CHANGED
|
@@ -1,12 +1,37 @@
|
|
|
1
|
+
import type { Request } from 'har-format';
|
|
1
2
|
export type { Request } from 'har-format';
|
|
2
|
-
|
|
3
|
+
/**
|
|
4
|
+
* List of available clients
|
|
5
|
+
*/
|
|
6
|
+
export type AvailableClients = [
|
|
7
|
+
'js/fetch',
|
|
8
|
+
'js/ofetch',
|
|
9
|
+
'node/fetch',
|
|
10
|
+
'node/ofetch',
|
|
11
|
+
'node/undici',
|
|
12
|
+
'shell/curl'
|
|
13
|
+
];
|
|
14
|
+
/** Programming language */
|
|
15
|
+
export type TargetId = AvailableClients[number] extends `${infer T}/${string}` ? T : never;
|
|
16
|
+
/** HTTP client */
|
|
17
|
+
export type ClientId<T extends string> = T extends TargetId ? Extract<AvailableClients[number], `${T}/${string}`> extends `${T}/${infer C}` ? C : never : never;
|
|
18
|
+
/** What any plugins needs to return */
|
|
19
|
+
export type Plugin = {
|
|
3
20
|
/** The language or environment. */
|
|
4
21
|
target: TargetId;
|
|
5
22
|
/** The identifier of the client. */
|
|
6
|
-
client: ClientId
|
|
23
|
+
client: ClientId<TargetId>;
|
|
7
24
|
/** The actual source code. */
|
|
8
|
-
|
|
25
|
+
generate: (request?: Partial<Request>, configuration?: PluginConfiguration) => string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Optional configuration for any plugin
|
|
29
|
+
*/
|
|
30
|
+
export type PluginConfiguration = {
|
|
31
|
+
/** Credentials to add HTTP Basic Authentication */
|
|
32
|
+
auth?: {
|
|
33
|
+
username: string;
|
|
34
|
+
password: string;
|
|
35
|
+
};
|
|
9
36
|
};
|
|
10
|
-
export type TargetId = 'node' | 'js';
|
|
11
|
-
export type ClientId = 'undici' | 'fetch' | 'ofetch';
|
|
12
37
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/core/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC,MAAM,MAAM,MAAM,GAAG;IACnB,mCAAmC;IACnC,MAAM,EAAE,QAAQ,CAAA;IAChB,oCAAoC;IACpC,MAAM,EAAE,QAAQ,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/core/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC,YAAY,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,UAAU;IACV,WAAW;IACX,YAAY;IACZ,aAAa;IACb,aAAa;IACb,YAAY;CACb,CAAA;AAED,2BAA2B;AAC3B,MAAM,MAAM,QAAQ,GAAG,gBAAgB,CAAC,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,IAAI,MAAM,EAAE,GAC1E,CAAC,GACD,KAAK,CAAA;AAET,kBAAkB;AAClB,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,QAAQ,GACvD,OAAO,CACL,gBAAgB,CAAC,MAAM,CAAC,EACxB,GAAG,CAAC,IAAI,MAAM,EAAE,CACjB,SAAS,GAAG,CAAC,IAAI,MAAM,CAAC,EAAE,GACzB,CAAC,GACD,KAAK,GACP,KAAK,CAAA;AAET,uCAAuC;AACvC,MAAM,MAAM,MAAM,GAAG;IACnB,mCAAmC;IACnC,MAAM,EAAE,QAAQ,CAAA;IAChB,oCAAoC;IACpC,MAAM,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAA;IAC1B,8BAA8B;IAC9B,QAAQ,EAAE,CACR,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAC1B,aAAa,CAAC,EAAE,mBAAmB,KAChC,MAAM,CAAA;CACZ,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG;IAChC,mDAAmD;IACnD,IAAI,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;CAC9C,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.test-d.d.ts","sourceRoot":"","sources":["../../src/core/types.test-d.ts"],"names":[],"mappings":""}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Converts an array of name/value pairs into an object with those mappings
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* arrayToObject([{ name: 'foo', value: 'bar' }]) // => { foo: 'bar' }
|
|
6
|
+
*/
|
|
2
7
|
export declare function arrayToObject(items: any): any;
|
|
3
8
|
//# sourceMappingURL=arrayToObject.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"arrayToObject.d.ts","sourceRoot":"","sources":["../../../src/core/utils/arrayToObject.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"arrayToObject.d.ts","sourceRoot":"","sources":["../../../src/core/utils/arrayToObject.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,GAAG,OAKvC"}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
/**
|
|
1
|
+
/**
|
|
2
|
+
* Converts an array of name/value pairs into an object with those mappings
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* arrayToObject([{ name: 'foo', value: 'bar' }]) // => { foo: 'bar' }
|
|
6
|
+
*/
|
|
2
7
|
function arrayToObject(items) {
|
|
3
8
|
return items.reduce((acc, item) => {
|
|
4
9
|
acc[item.name] = item.value;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"needsQuotes.d.ts","sourceRoot":"","sources":["../../../src/core/utils/needsQuotes.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,WAEtC"}
|
|
@@ -1,2 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts an object into a string representation with proper formatting and indentation
|
|
3
|
+
*
|
|
4
|
+
* Handles nested objects, arrays, and special string values
|
|
5
|
+
*/
|
|
1
6
|
export declare function objectToString(obj: Record<string, any>, indent?: number): string;
|
|
2
7
|
//# sourceMappingURL=objectToString.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"objectToString.d.ts","sourceRoot":"","sources":["../../../src/core/utils/objectToString.ts"],"names":[],"mappings":"AAEA,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,SAAI,GAAG,MAAM,CAyD3E"}
|
|
1
|
+
{"version":3,"file":"objectToString.d.ts","sourceRoot":"","sources":["../../../src/core/utils/objectToString.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,SAAI,GAAG,MAAM,CAyD3E"}
|
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { needsQuotes } from './needsQuotes.js';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Converts an object into a string representation with proper formatting and indentation
|
|
5
|
+
*
|
|
6
|
+
* Handles nested objects, arrays, and special string values
|
|
7
|
+
*/
|
|
3
8
|
function objectToString(obj, indent = 0) {
|
|
4
9
|
const parts = [];
|
|
5
10
|
const indentation = ' '.repeat(indent);
|
|
6
11
|
const innerIndentation = ' '.repeat(indent + 2);
|
|
7
12
|
for (const [key, value] of Object.entries(obj)) {
|
|
8
|
-
const formattedKey =
|
|
13
|
+
const formattedKey = needsQuotes(key) ? `'${key}'` : key;
|
|
9
14
|
if (Array.isArray(value)) {
|
|
10
15
|
const arrayString = value
|
|
11
16
|
.map((item) => {
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
1
|
+
import { type Plugin } from '../../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* js/fetch
|
|
4
|
+
*/
|
|
5
|
+
export declare const jsFetch: Plugin;
|
|
3
6
|
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/fetch/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/fetch/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAiC,MAAM,eAAe,CAAA;AAE1E;;GAEG;AACH,eAAO,MAAM,OAAO,EAAE,MA6ErB,CAAA"}
|
|
@@ -1,65 +1,69 @@
|
|
|
1
1
|
import { arrayToObject } from '../../../core/utils/arrayToObject.js';
|
|
2
2
|
import { objectToString } from '../../../core/utils/objectToString.js';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
// Remove undefined keys
|
|
38
|
-
Object.keys(options).forEach((key) => {
|
|
39
|
-
if (options[key] === undefined) {
|
|
40
|
-
delete options[key];
|
|
4
|
+
/**
|
|
5
|
+
* js/fetch
|
|
6
|
+
*/
|
|
7
|
+
const jsFetch = {
|
|
8
|
+
target: 'js',
|
|
9
|
+
client: 'fetch',
|
|
10
|
+
generate(request) {
|
|
11
|
+
// Defaults
|
|
12
|
+
const normalizedRequest = {
|
|
13
|
+
method: 'GET',
|
|
14
|
+
...request,
|
|
15
|
+
};
|
|
16
|
+
// Normalization
|
|
17
|
+
normalizedRequest.method = normalizedRequest.method.toUpperCase();
|
|
18
|
+
// Reset fetch defaults
|
|
19
|
+
const options = {
|
|
20
|
+
method: normalizedRequest.method === 'GET'
|
|
21
|
+
? undefined
|
|
22
|
+
: normalizedRequest.method,
|
|
23
|
+
};
|
|
24
|
+
// Query
|
|
25
|
+
const searchParams = new URLSearchParams(normalizedRequest.queryString
|
|
26
|
+
? arrayToObject(normalizedRequest.queryString)
|
|
27
|
+
: undefined);
|
|
28
|
+
const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
|
|
29
|
+
// Headers
|
|
30
|
+
if (normalizedRequest.headers?.length) {
|
|
31
|
+
options.headers = {};
|
|
32
|
+
normalizedRequest.headers.forEach((header) => {
|
|
33
|
+
options.headers[header.name] = header.value;
|
|
34
|
+
});
|
|
41
35
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
36
|
+
// Cookies
|
|
37
|
+
if (normalizedRequest.cookies?.length) {
|
|
38
|
+
options.headers = options.headers || {};
|
|
39
|
+
normalizedRequest.cookies.forEach((cookie) => {
|
|
40
|
+
options.headers['Set-Cookie'] = options.headers['Set-Cookie']
|
|
41
|
+
? `${options.headers['Set-Cookie']}; ${cookie.name}=${cookie.value}`
|
|
42
|
+
: `${cookie.name}=${cookie.value}`;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
// Remove undefined keys
|
|
46
|
+
Object.keys(options).forEach((key) => {
|
|
47
|
+
if (options[key] === undefined) {
|
|
48
|
+
delete options[key];
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
// Add body
|
|
52
|
+
if (normalizedRequest.postData) {
|
|
53
|
+
// Plain text
|
|
54
|
+
options.body = normalizedRequest.postData.text;
|
|
55
|
+
// JSON
|
|
56
|
+
if (normalizedRequest.postData.mimeType === 'application/json') {
|
|
57
|
+
options.body = `JSON.stringify(${objectToString(JSON.parse(options.body))})`;
|
|
58
|
+
}
|
|
50
59
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
target: 'js',
|
|
60
|
-
client: 'fetch',
|
|
61
|
-
code,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
60
|
+
// Transform to JSON
|
|
61
|
+
const jsonOptions = Object.keys(options).length
|
|
62
|
+
? `, ${objectToString(options)}`
|
|
63
|
+
: '';
|
|
64
|
+
// Code Template
|
|
65
|
+
return `fetch('${normalizedRequest.url}${queryString}'${jsonOptions})`;
|
|
66
|
+
},
|
|
67
|
+
};
|
|
64
68
|
|
|
65
|
-
export {
|
|
69
|
+
export { jsFetch };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { jsFetch } from './fetch.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { jsOfetch } from './ofetch.js';
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
1
|
+
import { type Plugin } from '../../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* js/ofetch
|
|
4
|
+
*/
|
|
5
|
+
export declare const jsOfetch: Plugin;
|
|
3
6
|
//# sourceMappingURL=ofetch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ofetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/ofetch/ofetch.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ofetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/ofetch/ofetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAiC,MAAM,eAAe,CAAA;AAE1E;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,MAmFtB,CAAA"}
|
|
@@ -1,70 +1,76 @@
|
|
|
1
1
|
import { arrayToObject } from '../../../core/utils/arrayToObject.js';
|
|
2
2
|
import { objectToString } from '../../../core/utils/objectToString.js';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
// Remove undefined keys
|
|
43
|
-
Object.keys(options).forEach((key) => {
|
|
44
|
-
if (options[key] === undefined) {
|
|
45
|
-
delete options[key];
|
|
4
|
+
/**
|
|
5
|
+
* js/ofetch
|
|
6
|
+
*/
|
|
7
|
+
const jsOfetch = {
|
|
8
|
+
target: 'js',
|
|
9
|
+
client: 'ofetch',
|
|
10
|
+
generate(request) {
|
|
11
|
+
// Defaults
|
|
12
|
+
const normalizedRequest = {
|
|
13
|
+
method: 'GET',
|
|
14
|
+
...request,
|
|
15
|
+
};
|
|
16
|
+
// Normalization
|
|
17
|
+
normalizedRequest.method = normalizedRequest.method.toUpperCase();
|
|
18
|
+
// Reset fetch defaults
|
|
19
|
+
const options = {
|
|
20
|
+
method: normalizedRequest.method === 'GET'
|
|
21
|
+
? undefined
|
|
22
|
+
: normalizedRequest.method,
|
|
23
|
+
};
|
|
24
|
+
// Query
|
|
25
|
+
const searchParams = new URLSearchParams(normalizedRequest.queryString
|
|
26
|
+
? arrayToObject(normalizedRequest.queryString)
|
|
27
|
+
: undefined);
|
|
28
|
+
if (searchParams.size) {
|
|
29
|
+
options.query = {};
|
|
30
|
+
searchParams.forEach((value, key) => {
|
|
31
|
+
options.query[key] = value;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
// Headers
|
|
35
|
+
if (normalizedRequest.headers?.length) {
|
|
36
|
+
options.headers = {};
|
|
37
|
+
normalizedRequest.headers.forEach((header) => {
|
|
38
|
+
options.headers[header.name] = header.value;
|
|
39
|
+
});
|
|
46
40
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
41
|
+
// Cookies
|
|
42
|
+
if (normalizedRequest.cookies?.length) {
|
|
43
|
+
options.headers = options.headers || {};
|
|
44
|
+
normalizedRequest.cookies.forEach((cookie) => {
|
|
45
|
+
options.headers['Set-Cookie'] = options.headers['Set-Cookie']
|
|
46
|
+
? `${options.headers['Set-Cookie']}; ${cookie.name}=${cookie.value}`
|
|
47
|
+
: `${cookie.name}=${cookie.value}`;
|
|
48
|
+
});
|
|
55
49
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
50
|
+
// Remove undefined keys
|
|
51
|
+
Object.keys(options).forEach((key) => {
|
|
52
|
+
if (options[key] === undefined) {
|
|
53
|
+
delete options[key];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
// Add body
|
|
57
|
+
if (normalizedRequest.postData) {
|
|
58
|
+
// Plain text
|
|
59
|
+
options.body = normalizedRequest.postData.text;
|
|
60
|
+
// JSON
|
|
61
|
+
if (normalizedRequest.postData.mimeType === 'application/json') {
|
|
62
|
+
options.body = JSON.parse(options.body);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
// Transform to JSON
|
|
66
|
+
const jsonOptions = Object.keys(options).length
|
|
67
|
+
? `, ${objectToString(options)}`
|
|
68
|
+
: '';
|
|
69
|
+
// Code Template
|
|
70
|
+
return `import { ofetch } from 'ofetch'
|
|
71
|
+
|
|
72
|
+
ofetch('${normalizedRequest.url}'${jsonOptions})`;
|
|
73
|
+
},
|
|
74
|
+
};
|
|
69
75
|
|
|
70
|
-
export {
|
|
76
|
+
export { jsOfetch };
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
1
|
+
import { type Plugin } from '../../../core/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* node/fetch
|
|
4
|
+
*/
|
|
5
|
+
export declare const nodeFetch: Plugin;
|
|
3
6
|
//# sourceMappingURL=fetch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/fetch/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/fetch/fetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAiC,MAAM,eAAe,CAAA;AAE1E;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MA6EvB,CAAA"}
|