@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 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 { undici } from '@scalar/snippetz/plugins/node/undici'
72
+ import { nodeUndici } from '@scalar/snippetz/plugins/node/undici'
73
73
 
74
- const source = undici({
74
+ const result = nodeUndici.generate({
75
75
  url: 'https://example.com',
76
76
  })
77
77
 
78
- console.log(source.code)
78
+ console.log(source)
79
79
 
80
80
  // import { request } from 'undici'
81
81
 
@@ -1,21 +1,28 @@
1
+ import type { Request } from 'har-format';
1
2
  export type { Request } from 'har-format';
2
- declare global {
3
- interface Clients {
4
- }
5
- }
6
- export type AddClient<T extends string, C extends string> = {
7
- [K in `${T}/${C}`]: C;
8
- };
9
- export type TargetId = keyof Clients & string extends `${infer T}/${string}` ? T : never;
10
- export type ClientId<T extends TargetId> = Clients[keyof Clients & `${T}/${string}`];
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 Source = {
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
- code: string;
25
+ generate: (request?: Partial<Request>, configuration?: PluginConfiguration) => string;
19
26
  };
20
27
  /**
21
28
  * Optional configuration for any plugin
@@ -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,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,OAAO;KAAG;CACrB;AAED,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI;KACzD,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,CAAC;CACtB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG,MAAM,OAAO,GAAG,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,MAAM,EAAE,GACxE,CAAC,GACD,KAAK,CAAA;AAET,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,QAAQ,IAAI,OAAO,CAAC,MAAM,OAAO,GAC9D,GAAG,CAAC,IAAI,MAAM,EAAE,CAAC,CAAA;AAEnB,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,IAAI,EAAE,MAAM,CAAA;CACb,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
+ {"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 AddClient, type Request, type Source } from '../../../core/index.js';
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 function fetch(request?: Partial<Request>): Source;
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,EACL,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,OAAQ,SAAQ,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC;KAAG;CACtD;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,MAAM,CA6ExD"}
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
- function fetch(request) {
8
- // Defaults
9
- const normalizedRequest = {
10
- method: 'GET',
11
- ...request,
12
- };
13
- // Normalization
14
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
15
- // Reset fetch defaults
16
- const options = {
17
- method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
18
- };
19
- // Query
20
- const searchParams = new URLSearchParams(normalizedRequest.queryString
21
- ? arrayToObject(normalizedRequest.queryString)
22
- : undefined);
23
- const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
24
- // Headers
25
- if (normalizedRequest.headers?.length) {
26
- options.headers = {};
27
- normalizedRequest.headers.forEach((header) => {
28
- options.headers[header.name] = header.value;
29
- });
30
- }
31
- // Cookies
32
- if (normalizedRequest.cookies?.length) {
33
- options.headers = options.headers || {};
34
- normalizedRequest.cookies.forEach((cookie) => {
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
- // Add body
47
- if (normalizedRequest.postData) {
48
- // Plain text
49
- options.body = normalizedRequest.postData.text;
50
- // JSON
51
- if (normalizedRequest.postData.mimeType === 'application/json') {
52
- options.body = `JSON.stringify(${objectToString(JSON.parse(options.body))})`;
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
- // Transform to JSON
56
- const jsonOptions = Object.keys(options).length
57
- ? `, ${objectToString(options)}`
58
- : '';
59
- // Code Template
60
- const code = `fetch('${normalizedRequest.url}${queryString}'${jsonOptions})`;
61
- return {
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 { fetch };
69
+ export { jsFetch };
@@ -1 +1 @@
1
- export { fetch } from './fetch.js';
1
+ export { jsFetch } from './fetch.js';
@@ -1 +1 @@
1
- export { ofetch } from './ofetch.js';
1
+ export { jsOfetch } from './ofetch.js';
@@ -1,10 +1,6 @@
1
- import { type AddClient, type Request, type Source } from '../../../core/index.js';
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 function ofetch(request?: Partial<Request>): Source;
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,EACL,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,OAAQ,SAAQ,SAAS,CAAC,IAAI,EAAE,QAAQ,CAAC;KAAG;CACvD;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,MAAM,CAmFzD"}
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
- function ofetch(request) {
8
- // Defaults
9
- const normalizedRequest = {
10
- method: 'GET',
11
- ...request,
12
- };
13
- // Normalization
14
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
15
- // Reset fetch defaults
16
- const options = {
17
- method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
18
- };
19
- // Query
20
- const searchParams = new URLSearchParams(normalizedRequest.queryString
21
- ? arrayToObject(normalizedRequest.queryString)
22
- : undefined);
23
- if (searchParams.size) {
24
- options.query = {};
25
- searchParams.forEach((value, key) => {
26
- options.query[key] = value;
27
- });
28
- }
29
- // Headers
30
- if (normalizedRequest.headers?.length) {
31
- options.headers = {};
32
- normalizedRequest.headers.forEach((header) => {
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
- // 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.parse(options.body);
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
- // Transform to JSON
61
- const jsonOptions = Object.keys(options).length
62
- ? `, ${objectToString(options)}`
63
- : '';
64
- // Code Template
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
- return {
69
- target: 'js',
70
- client: 'ofetch',
71
- code,
72
- };
73
- }
73
+ },
74
+ };
74
75
 
75
- export { ofetch };
76
+ export { jsOfetch };
@@ -1,10 +1,6 @@
1
- import { type AddClient, type Request, type Source } from '../../../core/index.js';
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 function fetch(request?: Partial<Request>): Source;
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,EACL,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,OAAQ,SAAQ,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC;KAAG;CACxD;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,MAAM,CA6ExD"}
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
- function fetch(request) {
8
- // Defaults
9
- const normalizedRequest = {
10
- method: 'GET',
11
- ...request,
12
- };
13
- // Normalization
14
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
15
- // Reset fetch defaults
16
- const options = {
17
- method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
18
- };
19
- // Query
20
- const searchParams = new URLSearchParams(normalizedRequest.queryString
21
- ? arrayToObject(normalizedRequest.queryString)
22
- : undefined);
23
- const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
24
- // Headers
25
- if (normalizedRequest.headers?.length) {
26
- options.headers = {};
27
- normalizedRequest.headers.forEach((header) => {
28
- options.headers[header.name] = header.value;
29
- });
30
- }
31
- // Cookies
32
- if (normalizedRequest.cookies?.length) {
33
- options.headers = options.headers || {};
34
- normalizedRequest.cookies.forEach((cookie) => {
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
- // Add body
47
- if (normalizedRequest.postData) {
48
- // Plain text
49
- options.body = normalizedRequest.postData.text;
50
- // JSON
51
- if (normalizedRequest.postData.mimeType === 'application/json') {
52
- options.body = `JSON.stringify(${objectToString(JSON.parse(options.body))})`;
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
- // Transform to JSON
56
- const jsonOptions = Object.keys(options).length
57
- ? `, ${objectToString(options)}`
58
- : '';
59
- // Code Template
60
- const code = `fetch('${normalizedRequest.url}${queryString}'${jsonOptions})`;
61
- return {
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 { fetch };
69
+ export { nodeFetch };
@@ -1 +1 @@
1
- export { fetch } from './fetch.js';
1
+ export { nodeFetch } from './fetch.js';
@@ -1 +1 @@
1
- export { ofetch } from './ofetch.js';
1
+ export { nodeOfetch } from './ofetch.js';
@@ -1,10 +1,6 @@
1
- import { type AddClient, type Request, type Source } from '../../../core/index.js';
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 function ofetch(request?: Partial<Request>): Source;
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,EACL,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,OAAQ,SAAQ,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC;KAAG;CACzD;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,MAAM,CAmFzD"}
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
- function ofetch(request) {
8
- // Defaults
9
- const normalizedRequest = {
10
- method: 'GET',
11
- ...request,
12
- };
13
- // Normalization
14
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
15
- // Reset fetch defaults
16
- const options = {
17
- method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
18
- };
19
- // Query
20
- const searchParams = new URLSearchParams(normalizedRequest.queryString
21
- ? arrayToObject(normalizedRequest.queryString)
22
- : undefined);
23
- if (searchParams.size) {
24
- options.query = {};
25
- searchParams.forEach((value, key) => {
26
- options.query[key] = value;
27
- });
28
- }
29
- // Headers
30
- if (normalizedRequest.headers?.length) {
31
- options.headers = {};
32
- normalizedRequest.headers.forEach((header) => {
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
- // 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.parse(options.body);
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
- // Transform to JSON
61
- const jsonOptions = Object.keys(options).length
62
- ? `, ${objectToString(options)}`
63
- : '';
64
- // Code Template
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
- return {
69
- target: 'node',
70
- client: 'ofetch',
71
- code,
72
- };
73
- }
73
+ },
74
+ };
74
75
 
75
- export { ofetch };
76
+ export { nodeOfetch };
@@ -1 +1 @@
1
- export { undici } from './undici.js';
1
+ export { nodeUndici } from './undici.js';
@@ -1,10 +1,6 @@
1
- import { type AddClient, type Request, type Source } from '../../../core/index.js';
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 function undici(request?: Partial<Request>): Source;
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,EACL,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,OAAQ,SAAQ,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC;KAAG;CACzD;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,MAAM,CA+EzD"}
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
- function undici(request) {
8
- // Defaults
9
- const normalizedRequest = {
10
- method: 'GET',
11
- ...request,
12
- };
13
- // Normalization
14
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
15
- // Reset undici defaults
16
- const options = {
17
- method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
18
- };
19
- // Query
20
- const searchParams = new URLSearchParams(normalizedRequest.queryString
21
- ? arrayToObject(normalizedRequest.queryString)
22
- : undefined);
23
- const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
24
- // Headers
25
- if (normalizedRequest.headers?.length) {
26
- options.headers = {};
27
- normalizedRequest.headers.forEach((header) => {
28
- options.headers[header.name] = header.value;
29
- });
30
- }
31
- // Cookies
32
- if (normalizedRequest.cookies?.length) {
33
- options.headers = options.headers || {};
34
- normalizedRequest.cookies.forEach((cookie) => {
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
- // Add body
47
- if (normalizedRequest.postData) {
48
- // Plain text
49
- options.body = normalizedRequest.postData.text;
50
- // JSON
51
- if (normalizedRequest.postData.mimeType === 'application/json') {
52
- options.body = `JSON.stringify(${objectToString(JSON.parse(options.body))})`;
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
- // Transform to JSON
56
- const jsonOptions = Object.keys(options).length
57
- ? `, ${objectToString(options)}`
58
- : '';
59
- // Code Template
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
- return {
64
- target: 'node',
65
- client: 'undici',
66
- code,
67
- };
68
- }
68
+ },
69
+ };
69
70
 
70
- export { undici };
71
+ export { nodeUndici };
@@ -1,10 +1,6 @@
1
- import type { AddClient, PluginConfiguration, Request, Source } from '../../../core/index.js';
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 function curl(request?: Partial<Request>, configuration?: PluginConfiguration): Source;
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,EACV,SAAS,EACT,mBAAmB,EACnB,OAAO,EACP,MAAM,EACP,MAAM,eAAe,CAAA;AAEtB,OAAO,CAAC,MAAM,CAAC;IAEb,UAAU,OAAQ,SAAQ,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC;KAAG;CACxD;AAED;;GAEG;AACH,wBAAgB,IAAI,CAClB,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,EAC1B,aAAa,CAAC,EAAE,mBAAmB,GAClC,MAAM,CAoHR"}
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
- function curl(request, configuration) {
5
- // Defaults
6
- const normalizedRequest = {
7
- method: 'GET',
8
- ...request,
9
- };
10
- // Normalization
11
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
12
- // Build curl command parts
13
- const parts = ['curl'];
14
- // URL (quote if has query parameters or special characters)
15
- const queryString = normalizedRequest.queryString?.length
16
- ? '?' +
17
- normalizedRequest.queryString
18
- .map((param) => {
19
- // Ensure both name and value are fully URI encoded
20
- const encodedName = encodeURIComponent(param.name);
21
- const encodedValue = encodeURIComponent(param.value);
22
- return `${encodedName}=${encodedValue}`;
23
- })
24
- .join('&')
25
- : '';
26
- const url = `${normalizedRequest.url}${queryString}`;
27
- const hasSpecialChars = /[\s<>[\]{}|\\^%]/.test(url);
28
- const urlPart = queryString || hasSpecialChars ? `'${url}'` : url;
29
- parts[0] = `curl ${urlPart}`;
30
- // Method
31
- if (normalizedRequest.method !== 'GET') {
32
- parts.push(`--request ${normalizedRequest.method}`);
33
- }
34
- // Basic Auth
35
- if (configuration?.auth?.username && configuration?.auth?.password) {
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
- else if (normalizedRequest.postData.mimeType === 'application/octet-stream') {
72
- parts.push(`--data-binary '${normalizedRequest.postData.text}'`);
37
+ // Basic Auth
38
+ if (configuration?.auth?.username && configuration?.auth?.password) {
39
+ parts.push(`--user '${configuration.auth.username}:${configuration.auth.password}'`);
73
40
  }
74
- else if (normalizedRequest.postData.mimeType ===
75
- 'application/x-www-form-urlencoded' &&
76
- normalizedRequest.postData.params) {
77
- // Handle URL-encoded form data
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
- else if (normalizedRequest.postData.mimeType === 'multipart/form-data' &&
83
- normalizedRequest.postData.params) {
84
- // Handle multipart form data
85
- normalizedRequest.postData.params.forEach((param) => {
86
- if (param.fileName !== undefined) {
87
- parts.push(`--form '${param.name}=@${param.fileName}'`);
88
- }
89
- else {
90
- parts.push(`--form '${param.name}=${param.value}'`);
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
- else {
95
- parts.push(`--data "${normalizedRequest.postData.text}"`);
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
- return {
99
- target: 'shell',
100
- client: 'curl',
101
- code: parts.join(' \\\n '),
102
- };
103
- }
101
+ return parts.join(' \\\n ');
102
+ },
103
+ };
104
104
 
105
- export { curl };
105
+ export { shellCurl };
@@ -1 +1 @@
1
- export { curl } from './curl.js';
1
+ export { shellCurl } from './curl.js';
@@ -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>, request: Partial<Request>): import("./core/index.js").Source | {
8
- code: string;
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(): ClientId<"js" | "node" | "shell">[];
9
+ clients(): ("fetch" | "ofetch" | "undici" | "curl")[];
13
10
  plugins(): {
14
11
  target: "js" | "node" | "shell";
15
- client: ClientId<"js" | "node" | "shell">;
12
+ client: "fetch" | "ofetch" | "undici" | "curl";
16
13
  }[];
17
- findPlugin<T extends TargetId>(target: T | string, client: ClientId<T> | string): typeof curl | undefined;
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
@@ -1 +1 @@
1
- {"version":3,"file":"snippetz.d.ts","sourceRoot":"","sources":["../src/snippetz.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AAMzD,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAA;AAE3C;;GAEG;AACH,wBAAgB,QAAQ;gBAKV,QAAQ,UACR,QAAQ,CAAC,QAAQ,CAAC,WACjB,OAAO,CAAC,OAAO,CAAC;;;UAYrB,CAAC,SAAS,QAAQ,UACd,CAAC,UACD,QAAQ,CAAC,CAAC,CAAC,WACV,OAAO,CAAC,OAAO,CAAC;;;;;;;eA0BhB,CAAC,SAAS,QAAQ,UACnB,CAAC,GAAG,MAAM,UACV,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM;cAQpB,CAAC,SAAS,QAAQ,UAClB,CAAC,GAAG,MAAM,UACV,QAAQ,CAAC,CAAC,CAAC,GAAG,MAAM;EAKjC"}
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 { undici } from './plugins/node/undici/undici.js';
2
- import { fetch } from './plugins/node/fetch/fetch.js';
3
- import { fetch as fetch$1 } from './plugins/js/fetch/fetch.js';
4
- import { ofetch } from './plugins/js/ofetch/ofetch.js';
5
- import { ofetch as ofetch$1 } from './plugins/node/ofetch/ofetch.js';
6
- import { curl } from './plugins/shell/curl/curl.js';
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 = [undici, fetch, fetch$1, ofetch, ofetch$1, curl];
12
+ const plugins = [
13
+ nodeUndici,
14
+ nodeFetch,
15
+ jsFetch,
16
+ jsOfetch,
17
+ nodeOfetch,
18
+ shellCurl,
19
+ ];
13
20
  return {
14
- get(target, client, request) {
15
- const plugin = this.findPlugin(target, client);
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, request)?.code;
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().target)
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().client);
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: details.target,
41
- client: details.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
- const details = plugin();
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) {
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "url": "https://github.com/scalar/scalar.git",
10
10
  "directory": "packages/snippetz"
11
11
  },
12
- "version": "0.2.5",
12
+ "version": "0.2.6",
13
13
  "engines": {
14
14
  "node": ">=18"
15
15
  },