@scalar/snippetz 0.2.5 → 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/types.d.ts +18 -11
- package/dist/core/types.d.ts.map +1 -1
- package/dist/plugins/js/fetch/fetch.d.ts +2 -6
- package/dist/plugins/js/fetch/fetch.d.ts.map +1 -1
- package/dist/plugins/js/fetch/fetch.js +60 -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 +2 -6
- package/dist/plugins/js/ofetch/ofetch.d.ts.map +1 -1
- package/dist/plugins/js/ofetch/ofetch.js +65 -64
- package/dist/plugins/node/fetch/fetch.d.ts +2 -6
- package/dist/plugins/node/fetch/fetch.d.ts.map +1 -1
- package/dist/plugins/node/fetch/fetch.js +60 -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 +2 -6
- package/dist/plugins/node/ofetch/ofetch.d.ts.map +1 -1
- package/dist/plugins/node/ofetch/ofetch.js +65 -64
- package/dist/plugins/node/undici/index.js +1 -1
- package/dist/plugins/node/undici/undici.d.ts +2 -6
- package/dist/plugins/node/undici/undici.d.ts.map +1 -1
- package/dist/plugins/node/undici/undici.js +60 -59
- package/dist/plugins/shell/curl/curl.d.ts +2 -6
- package/dist/plugins/shell/curl/curl.d.ts.map +1 -1
- package/dist/plugins/shell/curl/curl.js +95 -95
- package/dist/plugins/shell/curl/index.js +1 -1
- package/dist/snippetz.d.ts +6 -9
- package/dist/snippetz.d.ts.map +1 -1
- package/dist/snippetz.js +22 -23
- package/package.json +1 -1
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/types.d.ts
CHANGED
|
@@ -1,21 +1,28 @@
|
|
|
1
|
+
import type { Request } from 'har-format';
|
|
1
2
|
export type { Request } from 'har-format';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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;
|
|
11
18
|
/** What any plugins needs to return */
|
|
12
|
-
export type
|
|
19
|
+
export type Plugin = {
|
|
13
20
|
/** The language or environment. */
|
|
14
21
|
target: TargetId;
|
|
15
22
|
/** The identifier of the client. */
|
|
16
23
|
client: ClientId<TargetId>;
|
|
17
24
|
/** The actual source code. */
|
|
18
|
-
|
|
25
|
+
generate: (request?: Partial<Request>, configuration?: PluginConfiguration) => string;
|
|
19
26
|
};
|
|
20
27
|
/**
|
|
21
28
|
* Optional configuration for any plugin
|
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,
|
|
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"}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
declare global {
|
|
3
|
-
interface Clients extends AddClient<'js', 'fetch'> {
|
|
4
|
-
}
|
|
5
|
-
}
|
|
1
|
+
import { type Plugin } from '../../../core/index.js';
|
|
6
2
|
/**
|
|
7
3
|
* js/fetch
|
|
8
4
|
*/
|
|
9
|
-
export declare
|
|
5
|
+
export declare const jsFetch: Plugin;
|
|
10
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"}
|
|
@@ -4,65 +4,66 @@ import { objectToString } from '../../../core/utils/objectToString.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* js/fetch
|
|
6
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
|
-
options.headers['Set-Cookie'] = options.headers['Set-Cookie']
|
|
36
|
-
? `${options.headers['Set-Cookie']}; ${cookie.name}=${cookie.value}`
|
|
37
|
-
: `${cookie.name}=${cookie.value}`;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
// Remove undefined keys
|
|
41
|
-
Object.keys(options).forEach((key) => {
|
|
42
|
-
if (options[key] === undefined) {
|
|
43
|
-
delete options[key];
|
|
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
|
+
});
|
|
44
35
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
}
|
|
53
59
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
target: 'js',
|
|
63
|
-
client: 'fetch',
|
|
64
|
-
code,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
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
|
+
};
|
|
67
68
|
|
|
68
|
-
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,10 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
declare global {
|
|
3
|
-
interface Clients extends AddClient<'js', 'ofetch'> {
|
|
4
|
-
}
|
|
5
|
-
}
|
|
1
|
+
import { type Plugin } from '../../../core/index.js';
|
|
6
2
|
/**
|
|
7
3
|
* js/ofetch
|
|
8
4
|
*/
|
|
9
|
-
export declare
|
|
5
|
+
export declare const jsOfetch: Plugin;
|
|
10
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"}
|
|
@@ -4,72 +4,73 @@ import { objectToString } from '../../../core/utils/objectToString.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* js/ofetch
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
searchParams
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
options.headers[header.name] = header.value;
|
|
34
|
-
});
|
|
35
|
-
}
|
|
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];
|
|
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
|
+
});
|
|
49
33
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
34
|
+
// Headers
|
|
35
|
+
if (normalizedRequest.headers?.length) {
|
|
36
|
+
options.headers = {};
|
|
37
|
+
normalizedRequest.headers.forEach((header) => {
|
|
38
|
+
options.headers[header.name] = header.value;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
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
|
+
});
|
|
49
|
+
}
|
|
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
|
+
}
|
|
58
64
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const code = `import { ofetch } from 'ofetch'
|
|
65
|
+
// Transform to JSON
|
|
66
|
+
const jsonOptions = Object.keys(options).length
|
|
67
|
+
? `, ${objectToString(options)}`
|
|
68
|
+
: '';
|
|
69
|
+
// Code Template
|
|
70
|
+
return `import { ofetch } from 'ofetch'
|
|
66
71
|
|
|
67
72
|
ofetch('${normalizedRequest.url}'${jsonOptions})`;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
client: 'ofetch',
|
|
71
|
-
code,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
73
|
+
},
|
|
74
|
+
};
|
|
74
75
|
|
|
75
|
-
export {
|
|
76
|
+
export { jsOfetch };
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
declare global {
|
|
3
|
-
interface Clients extends AddClient<'node', 'fetch'> {
|
|
4
|
-
}
|
|
5
|
-
}
|
|
1
|
+
import { type Plugin } from '../../../core/index.js';
|
|
6
2
|
/**
|
|
7
3
|
* node/fetch
|
|
8
4
|
*/
|
|
9
|
-
export declare
|
|
5
|
+
export declare const nodeFetch: Plugin;
|
|
10
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"}
|
|
@@ -4,65 +4,66 @@ import { objectToString } from '../../../core/utils/objectToString.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* node/fetch
|
|
6
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
|
-
options.headers['Set-Cookie'] = options.headers['Set-Cookie']
|
|
36
|
-
? `${options.headers['Set-Cookie']}; ${cookie.name}=${cookie.value}`
|
|
37
|
-
: `${cookie.name}=${cookie.value}`;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
// Remove undefined keys
|
|
41
|
-
Object.keys(options).forEach((key) => {
|
|
42
|
-
if (options[key] === undefined) {
|
|
43
|
-
delete options[key];
|
|
7
|
+
const nodeFetch = {
|
|
8
|
+
target: 'node',
|
|
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
|
+
});
|
|
44
35
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
}
|
|
53
59
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
target: 'node',
|
|
63
|
-
client: 'fetch',
|
|
64
|
-
code,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
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
|
+
};
|
|
67
68
|
|
|
68
|
-
export {
|
|
69
|
+
export { nodeFetch };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { nodeFetch } from './fetch.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { nodeOfetch } from './ofetch.js';
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
declare global {
|
|
3
|
-
interface Clients extends AddClient<'node', 'ofetch'> {
|
|
4
|
-
}
|
|
5
|
-
}
|
|
1
|
+
import { type Plugin } from '../../../core/index.js';
|
|
6
2
|
/**
|
|
7
3
|
* node/ofetch
|
|
8
4
|
*/
|
|
9
|
-
export declare
|
|
5
|
+
export declare const nodeOfetch: Plugin;
|
|
10
6
|
//# sourceMappingURL=ofetch.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ofetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/ofetch/ofetch.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ofetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/ofetch/ofetch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAiC,MAAM,eAAe,CAAA;AAE1E;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MAmFxB,CAAA"}
|
|
@@ -4,72 +4,73 @@ import { objectToString } from '../../../core/utils/objectToString.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* node/ofetch
|
|
6
6
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
searchParams
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
options.headers[header.name] = header.value;
|
|
34
|
-
});
|
|
35
|
-
}
|
|
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];
|
|
7
|
+
const nodeOfetch = {
|
|
8
|
+
target: 'node',
|
|
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
|
+
});
|
|
49
33
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
34
|
+
// Headers
|
|
35
|
+
if (normalizedRequest.headers?.length) {
|
|
36
|
+
options.headers = {};
|
|
37
|
+
normalizedRequest.headers.forEach((header) => {
|
|
38
|
+
options.headers[header.name] = header.value;
|
|
39
|
+
});
|
|
40
|
+
}
|
|
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
|
+
});
|
|
49
|
+
}
|
|
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
|
+
}
|
|
58
64
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const code = `import { ofetch } from 'ofetch'
|
|
65
|
+
// Transform to JSON
|
|
66
|
+
const jsonOptions = Object.keys(options).length
|
|
67
|
+
? `, ${objectToString(options)}`
|
|
68
|
+
: '';
|
|
69
|
+
// Code Template
|
|
70
|
+
return `import { ofetch } from 'ofetch'
|
|
66
71
|
|
|
67
72
|
ofetch('${normalizedRequest.url}'${jsonOptions})`;
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
client: 'ofetch',
|
|
71
|
-
code,
|
|
72
|
-
};
|
|
73
|
-
}
|
|
73
|
+
},
|
|
74
|
+
};
|
|
74
75
|
|
|
75
|
-
export {
|
|
76
|
+
export { nodeOfetch };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { nodeUndici } from './undici.js';
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
declare global {
|
|
3
|
-
interface Clients extends AddClient<'node', 'undici'> {
|
|
4
|
-
}
|
|
5
|
-
}
|
|
1
|
+
import { type Plugin } from '../../../core/index.js';
|
|
6
2
|
/**
|
|
7
3
|
* node/undici
|
|
8
4
|
*/
|
|
9
|
-
export declare
|
|
5
|
+
export declare const nodeUndici: Plugin;
|
|
10
6
|
//# sourceMappingURL=undici.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"undici.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/undici/undici.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"undici.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/undici/undici.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAiC,MAAM,eAAe,CAAA;AAE1E;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,MA+ExB,CAAA"}
|
|
@@ -4,67 +4,68 @@ import { objectToString } from '../../../core/utils/objectToString.js';
|
|
|
4
4
|
/**
|
|
5
5
|
* node/undici
|
|
6
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
|
-
options.headers['Set-Cookie'] = options.headers['Set-Cookie']
|
|
36
|
-
? `${options.headers['Set-Cookie']}; ${cookie.name}=${cookie.value}`
|
|
37
|
-
: `${cookie.name}=${cookie.value}`;
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
// Remove undefined keys
|
|
41
|
-
Object.keys(options).forEach((key) => {
|
|
42
|
-
if (options[key] === undefined) {
|
|
43
|
-
delete options[key];
|
|
7
|
+
const nodeUndici = {
|
|
8
|
+
target: 'node',
|
|
9
|
+
client: 'undici',
|
|
10
|
+
generate(request) {
|
|
11
|
+
// Defaults
|
|
12
|
+
const normalizedRequest = {
|
|
13
|
+
method: 'GET',
|
|
14
|
+
...request,
|
|
15
|
+
};
|
|
16
|
+
// Normalization
|
|
17
|
+
normalizedRequest.method = normalizedRequest.method.toUpperCase();
|
|
18
|
+
// Reset undici 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
|
+
});
|
|
44
35
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
}
|
|
53
59
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const code = `import { request } from 'undici'
|
|
60
|
+
// Transform to JSON
|
|
61
|
+
const jsonOptions = Object.keys(options).length
|
|
62
|
+
? `, ${objectToString(options)}`
|
|
63
|
+
: '';
|
|
64
|
+
// Code Template
|
|
65
|
+
return `import { request } from 'undici'
|
|
61
66
|
|
|
62
67
|
const { statusCode, body } = await request('${normalizedRequest.url}${queryString}'${jsonOptions})`;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
client: 'undici',
|
|
66
|
-
code,
|
|
67
|
-
};
|
|
68
|
-
}
|
|
68
|
+
},
|
|
69
|
+
};
|
|
69
70
|
|
|
70
|
-
export {
|
|
71
|
+
export { nodeUndici };
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
declare global {
|
|
3
|
-
interface Clients extends AddClient<'shell', 'curl'> {
|
|
4
|
-
}
|
|
5
|
-
}
|
|
1
|
+
import type { Plugin } from '../../../core/index.js';
|
|
6
2
|
/**
|
|
7
3
|
* shell/curl
|
|
8
4
|
*/
|
|
9
|
-
export declare
|
|
5
|
+
export declare const shellCurl: Plugin;
|
|
10
6
|
//# sourceMappingURL=curl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"curl.d.ts","sourceRoot":"","sources":["../../../../src/plugins/shell/curl/curl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"curl.d.ts","sourceRoot":"","sources":["../../../../src/plugins/shell/curl/curl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAA;AAE3C;;GAEG;AACH,eAAO,MAAM,SAAS,EAAE,MAoHvB,CAAA"}
|
|
@@ -1,105 +1,105 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* shell/curl
|
|
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
|
-
parts
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
parts.push(`--user '${configuration.auth.username}:${configuration.auth.password}'`);
|
|
37
|
-
}
|
|
38
|
-
// Headers
|
|
39
|
-
if (normalizedRequest.headers?.length) {
|
|
40
|
-
normalizedRequest.headers.forEach((header) => {
|
|
41
|
-
parts.push(`--header '${header.name}: ${header.value}'`);
|
|
42
|
-
});
|
|
43
|
-
// Add compressed flag if Accept-Encoding header includes compression
|
|
44
|
-
const acceptEncoding = normalizedRequest.headers.find((header) => header.name.toLowerCase() === 'accept-encoding');
|
|
45
|
-
if (acceptEncoding && /gzip|deflate/.test(acceptEncoding.value)) {
|
|
46
|
-
parts.push('--compressed');
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
// Cookies
|
|
50
|
-
if (normalizedRequest.cookies?.length) {
|
|
51
|
-
const cookieString = normalizedRequest.cookies
|
|
52
|
-
.map((cookie) => {
|
|
53
|
-
// Encode both cookie name and value to handle special characters
|
|
54
|
-
const encodedName = encodeURIComponent(cookie.name);
|
|
55
|
-
const encodedValue = encodeURIComponent(cookie.value);
|
|
56
|
-
return `${encodedName}=${encodedValue}`;
|
|
57
|
-
})
|
|
58
|
-
.join('; ');
|
|
59
|
-
parts.push(`--cookie '${cookieString}'`);
|
|
60
|
-
}
|
|
61
|
-
// Body
|
|
62
|
-
if (normalizedRequest.postData) {
|
|
63
|
-
if (normalizedRequest.postData.mimeType === 'application/json') {
|
|
64
|
-
// Pretty print JSON data
|
|
65
|
-
if (normalizedRequest.postData.text) {
|
|
66
|
-
const jsonData = JSON.parse(normalizedRequest.postData.text);
|
|
67
|
-
const prettyJson = JSON.stringify(jsonData, null, 2);
|
|
68
|
-
parts.push(`--data '${prettyJson}'`);
|
|
69
|
-
}
|
|
4
|
+
const shellCurl = {
|
|
5
|
+
target: 'shell',
|
|
6
|
+
client: 'curl',
|
|
7
|
+
generate(request, configuration) {
|
|
8
|
+
// Defaults
|
|
9
|
+
const normalizedRequest = {
|
|
10
|
+
method: 'GET',
|
|
11
|
+
...request,
|
|
12
|
+
};
|
|
13
|
+
// Normalization
|
|
14
|
+
normalizedRequest.method = normalizedRequest.method.toUpperCase();
|
|
15
|
+
// Build curl command parts
|
|
16
|
+
const parts = ['curl'];
|
|
17
|
+
// URL (quote if has query parameters or special characters)
|
|
18
|
+
const queryString = normalizedRequest.queryString?.length
|
|
19
|
+
? '?' +
|
|
20
|
+
normalizedRequest.queryString
|
|
21
|
+
.map((param) => {
|
|
22
|
+
// Ensure both name and value are fully URI encoded
|
|
23
|
+
const encodedName = encodeURIComponent(param.name);
|
|
24
|
+
const encodedValue = encodeURIComponent(param.value);
|
|
25
|
+
return `${encodedName}=${encodedValue}`;
|
|
26
|
+
})
|
|
27
|
+
.join('&')
|
|
28
|
+
: '';
|
|
29
|
+
const url = `${normalizedRequest.url}${queryString}`;
|
|
30
|
+
const hasSpecialChars = /[\s<>[\]{}|\\^%]/.test(url);
|
|
31
|
+
const urlPart = queryString || hasSpecialChars ? `'${url}'` : url;
|
|
32
|
+
parts[0] = `curl ${urlPart}`;
|
|
33
|
+
// Method
|
|
34
|
+
if (normalizedRequest.method !== 'GET') {
|
|
35
|
+
parts.push(`--request ${normalizedRequest.method}`);
|
|
70
36
|
}
|
|
71
|
-
|
|
72
|
-
|
|
37
|
+
// Basic Auth
|
|
38
|
+
if (configuration?.auth?.username && configuration?.auth?.password) {
|
|
39
|
+
parts.push(`--user '${configuration.auth.username}:${configuration.auth.password}'`);
|
|
73
40
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
normalizedRequest.
|
|
77
|
-
|
|
78
|
-
normalizedRequest.postData.params.forEach((param) => {
|
|
79
|
-
parts.push(`--data-urlencode '${encodeURIComponent(param.name)}=${param.value}'`);
|
|
41
|
+
// Headers
|
|
42
|
+
if (normalizedRequest.headers?.length) {
|
|
43
|
+
normalizedRequest.headers.forEach((header) => {
|
|
44
|
+
parts.push(`--header '${header.name}: ${header.value}'`);
|
|
80
45
|
});
|
|
46
|
+
// Add compressed flag if Accept-Encoding header includes compression
|
|
47
|
+
const acceptEncoding = normalizedRequest.headers.find((header) => header.name.toLowerCase() === 'accept-encoding');
|
|
48
|
+
if (acceptEncoding && /gzip|deflate/.test(acceptEncoding.value)) {
|
|
49
|
+
parts.push('--compressed');
|
|
50
|
+
}
|
|
81
51
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
});
|
|
52
|
+
// Cookies
|
|
53
|
+
if (normalizedRequest.cookies?.length) {
|
|
54
|
+
const cookieString = normalizedRequest.cookies
|
|
55
|
+
.map((cookie) => {
|
|
56
|
+
// Encode both cookie name and value to handle special characters
|
|
57
|
+
const encodedName = encodeURIComponent(cookie.name);
|
|
58
|
+
const encodedValue = encodeURIComponent(cookie.value);
|
|
59
|
+
return `${encodedName}=${encodedValue}`;
|
|
60
|
+
})
|
|
61
|
+
.join('; ');
|
|
62
|
+
parts.push(`--cookie '${cookieString}'`);
|
|
93
63
|
}
|
|
94
|
-
|
|
95
|
-
|
|
64
|
+
// Body
|
|
65
|
+
if (normalizedRequest.postData) {
|
|
66
|
+
if (normalizedRequest.postData.mimeType === 'application/json') {
|
|
67
|
+
// Pretty print JSON data
|
|
68
|
+
if (normalizedRequest.postData.text) {
|
|
69
|
+
const jsonData = JSON.parse(normalizedRequest.postData.text);
|
|
70
|
+
const prettyJson = JSON.stringify(jsonData, null, 2);
|
|
71
|
+
parts.push(`--data '${prettyJson}'`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if (normalizedRequest.postData.mimeType === 'application/octet-stream') {
|
|
75
|
+
parts.push(`--data-binary '${normalizedRequest.postData.text}'`);
|
|
76
|
+
}
|
|
77
|
+
else if (normalizedRequest.postData.mimeType ===
|
|
78
|
+
'application/x-www-form-urlencoded' &&
|
|
79
|
+
normalizedRequest.postData.params) {
|
|
80
|
+
// Handle URL-encoded form data
|
|
81
|
+
normalizedRequest.postData.params.forEach((param) => {
|
|
82
|
+
parts.push(`--data-urlencode '${encodeURIComponent(param.name)}=${param.value}'`);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else if (normalizedRequest.postData.mimeType === 'multipart/form-data' &&
|
|
86
|
+
normalizedRequest.postData.params) {
|
|
87
|
+
// Handle multipart form data
|
|
88
|
+
normalizedRequest.postData.params.forEach((param) => {
|
|
89
|
+
if (param.fileName !== undefined) {
|
|
90
|
+
parts.push(`--form '${param.name}=@${param.fileName}'`);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
parts.push(`--form '${param.name}=${param.value}'`);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
parts.push(`--data "${normalizedRequest.postData.text}"`);
|
|
99
|
+
}
|
|
96
100
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
client: 'curl',
|
|
101
|
-
code: parts.join(' \\\n '),
|
|
102
|
-
};
|
|
103
|
-
}
|
|
101
|
+
return parts.join(' \\\n ');
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
104
|
|
|
105
|
-
export {
|
|
105
|
+
export { shellCurl };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { shellCurl } from './curl.js';
|
package/dist/snippetz.d.ts
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
import type { ClientId, Request, TargetId } from './core/index.js';
|
|
2
|
-
import { curl } from './plugins/shell/curl/index.js';
|
|
1
|
+
import type { ClientId, Plugin, Request, TargetId } from './core/index.js';
|
|
3
2
|
/**
|
|
4
3
|
* Generate code examples for HAR requests
|
|
5
4
|
*/
|
|
6
5
|
export declare function snippetz(): {
|
|
7
|
-
get(target: TargetId, client: ClientId<TargetId
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
print<T extends TargetId>(target: T, client: ClientId<T>, request: Partial<Request>): string;
|
|
6
|
+
get(target: TargetId, client: ClientId<TargetId>): Plugin | undefined;
|
|
7
|
+
print<T extends TargetId>(target: T, client: ClientId<T>, request: Partial<Request>): string | undefined;
|
|
11
8
|
targets(): ("js" | "node" | "shell")[];
|
|
12
|
-
clients():
|
|
9
|
+
clients(): ("fetch" | "ofetch" | "undici" | "curl")[];
|
|
13
10
|
plugins(): {
|
|
14
11
|
target: "js" | "node" | "shell";
|
|
15
|
-
client:
|
|
12
|
+
client: "fetch" | "ofetch" | "undici" | "curl";
|
|
16
13
|
}[];
|
|
17
|
-
findPlugin<T extends TargetId>(target: T | string, client: ClientId<T> | string):
|
|
14
|
+
findPlugin<T extends TargetId>(target: T | string, client: ClientId<T> | string): Plugin | undefined;
|
|
18
15
|
hasPlugin<T extends TargetId>(target: T | string, client: ClientId<T> | string): boolean;
|
|
19
16
|
};
|
|
20
17
|
//# sourceMappingURL=snippetz.d.ts.map
|
package/dist/snippetz.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"snippetz.d.ts","sourceRoot":"","sources":["../src/snippetz.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"snippetz.d.ts","sourceRoot":"","sources":["../src/snippetz.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AAQjE;;GAEG;AACH,wBAAgB,QAAQ;gBAWR,QAAQ,UAAU,QAAQ,CAAC,QAAQ,CAAC;UAG1C,CAAC,SAAS,QAAQ,UACd,CAAC,UACD,QAAQ,CAAC,CAAC,CAAC,WACV,OAAO,CAAC,OAAO,CAAC;;;;;;;eAwBhB,CAAC,SAAS,QAAQ,UACnB,CAAC,GAAG,MAAM,UACV,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM;cAMpB,CAAC,SAAS,QAAQ,UAClB,CAAC,GAAG,MAAM,UACV,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM;EAKjC"}
|
package/dist/snippetz.js
CHANGED
|
@@ -1,51 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { nodeUndici } from './plugins/node/undici/undici.js';
|
|
2
|
+
import { nodeFetch } from './plugins/node/fetch/fetch.js';
|
|
3
|
+
import { jsFetch } from './plugins/js/fetch/fetch.js';
|
|
4
|
+
import { jsOfetch } from './plugins/js/ofetch/ofetch.js';
|
|
5
|
+
import { nodeOfetch } from './plugins/node/ofetch/ofetch.js';
|
|
6
|
+
import { shellCurl } from './plugins/shell/curl/curl.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Generate code examples for HAR requests
|
|
10
10
|
*/
|
|
11
11
|
function snippetz() {
|
|
12
|
-
const plugins = [
|
|
12
|
+
const plugins = [
|
|
13
|
+
nodeUndici,
|
|
14
|
+
nodeFetch,
|
|
15
|
+
jsFetch,
|
|
16
|
+
jsOfetch,
|
|
17
|
+
nodeOfetch,
|
|
18
|
+
shellCurl,
|
|
19
|
+
];
|
|
13
20
|
return {
|
|
14
|
-
get(target, client
|
|
15
|
-
|
|
16
|
-
if (plugin) {
|
|
17
|
-
return plugin(request);
|
|
18
|
-
}
|
|
19
|
-
return {
|
|
20
|
-
code: '',
|
|
21
|
-
};
|
|
21
|
+
get(target, client) {
|
|
22
|
+
return this.findPlugin(target, client);
|
|
22
23
|
},
|
|
23
24
|
print(target, client, request) {
|
|
24
|
-
return this.get(target, client
|
|
25
|
+
return this.get(target, client)?.generate(request);
|
|
25
26
|
},
|
|
26
27
|
targets() {
|
|
27
28
|
return (plugins
|
|
28
29
|
// all targets
|
|
29
|
-
.map((plugin) => plugin
|
|
30
|
+
.map((plugin) => plugin.target)
|
|
30
31
|
// unique values
|
|
31
32
|
.filter((value, index, self) => self.indexOf(value) === index));
|
|
32
33
|
},
|
|
33
34
|
clients() {
|
|
34
|
-
return plugins.map((plugin) => plugin
|
|
35
|
+
return plugins.map((plugin) => plugin.client);
|
|
35
36
|
},
|
|
36
37
|
plugins() {
|
|
37
38
|
return plugins.map((plugin) => {
|
|
38
|
-
const details = plugin();
|
|
39
39
|
return {
|
|
40
|
-
target:
|
|
41
|
-
client:
|
|
40
|
+
target: plugin.target,
|
|
41
|
+
client: plugin.client,
|
|
42
42
|
};
|
|
43
43
|
});
|
|
44
44
|
},
|
|
45
45
|
findPlugin(target, client) {
|
|
46
46
|
return plugins.find((plugin) => {
|
|
47
|
-
|
|
48
|
-
return details.target === target && details.client === client;
|
|
47
|
+
return plugin.target === target && plugin.client === client;
|
|
49
48
|
});
|
|
50
49
|
},
|
|
51
50
|
hasPlugin(target, client) {
|