@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.
Files changed (50) hide show
  1. package/README.md +3 -3
  2. package/dist/core/index.d.ts +1 -1
  3. package/dist/core/index.d.ts.map +1 -1
  4. package/dist/core/index.js +1 -1
  5. package/dist/core/types.d.ts +30 -5
  6. package/dist/core/types.d.ts.map +1 -1
  7. package/dist/core/types.test-d.d.ts +2 -0
  8. package/dist/core/types.test-d.d.ts.map +1 -0
  9. package/dist/core/utils/arrayToObject.d.ts +6 -1
  10. package/dist/core/utils/arrayToObject.d.ts.map +1 -1
  11. package/dist/core/utils/arrayToObject.js +6 -1
  12. package/dist/core/utils/needsQuotes.d.ts +7 -0
  13. package/dist/core/utils/needsQuotes.d.ts.map +1 -0
  14. package/dist/core/utils/needsQuotes.js +10 -0
  15. package/dist/core/utils/objectToString.d.ts +5 -0
  16. package/dist/core/utils/objectToString.d.ts.map +1 -1
  17. package/dist/core/utils/objectToString.js +7 -2
  18. package/dist/plugins/js/fetch/fetch.d.ts +5 -2
  19. package/dist/plugins/js/fetch/fetch.d.ts.map +1 -1
  20. package/dist/plugins/js/fetch/fetch.js +63 -59
  21. package/dist/plugins/js/fetch/index.js +1 -1
  22. package/dist/plugins/js/ofetch/index.js +1 -1
  23. package/dist/plugins/js/ofetch/ofetch.d.ts +5 -2
  24. package/dist/plugins/js/ofetch/ofetch.d.ts.map +1 -1
  25. package/dist/plugins/js/ofetch/ofetch.js +70 -64
  26. package/dist/plugins/node/fetch/fetch.d.ts +5 -2
  27. package/dist/plugins/node/fetch/fetch.d.ts.map +1 -1
  28. package/dist/plugins/node/fetch/fetch.js +63 -59
  29. package/dist/plugins/node/fetch/index.js +1 -1
  30. package/dist/plugins/node/ofetch/index.js +1 -1
  31. package/dist/plugins/node/ofetch/ofetch.d.ts +5 -2
  32. package/dist/plugins/node/ofetch/ofetch.d.ts.map +1 -1
  33. package/dist/plugins/node/ofetch/ofetch.js +70 -64
  34. package/dist/plugins/node/undici/index.js +1 -1
  35. package/dist/plugins/node/undici/undici.d.ts +5 -2
  36. package/dist/plugins/node/undici/undici.d.ts.map +1 -1
  37. package/dist/plugins/node/undici/undici.js +63 -59
  38. package/dist/plugins/shell/curl/curl.d.ts +6 -0
  39. package/dist/plugins/shell/curl/curl.d.ts.map +1 -0
  40. package/dist/plugins/shell/curl/curl.js +105 -0
  41. package/dist/plugins/shell/curl/index.d.ts +2 -0
  42. package/dist/plugins/shell/curl/index.d.ts.map +1 -0
  43. package/dist/plugins/shell/curl/index.js +1 -0
  44. package/dist/snippetz.d.ts +9 -12
  45. package/dist/snippetz.d.ts.map +1 -1
  46. package/dist/snippetz.js +22 -22
  47. package/package.json +7 -2
  48. package/dist/core/utils/isKeyNeedsQuotes.d.ts +0 -2
  49. package/dist/core/utils/isKeyNeedsQuotes.d.ts.map +0 -1
  50. 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 { 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,5 +1,5 @@
1
1
  export * from './utils/arrayToObject.js';
2
- export * from './utils/isKeyNeedsQuotes.js';
2
+ export * from './utils/needsQuotes.js';
3
3
  export * from './utils/objectToString.js';
4
4
  export * from './types';
5
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAA;AACrC,cAAc,0BAA0B,CAAA;AACxC,cAAc,wBAAwB,CAAA;AAEtC,cAAc,SAAS,CAAA"}
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"}
@@ -1,3 +1,3 @@
1
1
  export { arrayToObject } from './utils/arrayToObject.js';
2
- export { isKeyNeedsQuotes } from './utils/isKeyNeedsQuotes.js';
2
+ export { needsQuotes } from './utils/needsQuotes.js';
3
3
  export { objectToString } from './utils/objectToString.js';
@@ -1,12 +1,37 @@
1
+ import type { Request } from 'har-format';
1
2
  export type { Request } from 'har-format';
2
- export type Source = {
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
- code: string;
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
@@ -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;IAChB,8BAA8B;IAC9B,IAAI,EAAE,MAAM,CAAA;CACb,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG,MAAM,GAAG,IAAI,CAAA;AAEpC,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,OAAO,GAAG,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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.test-d.d.ts.map
@@ -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
- /** Helper function to map { name: 'foo', value: 'bar' } to { foo: 'bar' } */
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,6EAA6E;AAC7E,wBAAgB,aAAa,CAAC,KAAK,EAAE,GAAG,OAKvC"}
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
- /** Helper function to map { name: 'foo', value: 'bar' } to { foo: 'bar' } */
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,7 @@
1
+ /**
2
+ * Checks if a key needs to be wrapped in quotes when used as an object property
3
+ *
4
+ * Returns true if the key contains spaces or hyphens
5
+ */
6
+ export declare function needsQuotes(key: string): boolean;
7
+ //# sourceMappingURL=needsQuotes.d.ts.map
@@ -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"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Checks if a key needs to be wrapped in quotes when used as an object property
3
+ *
4
+ * Returns true if the key contains spaces or hyphens
5
+ */
6
+ function needsQuotes(key) {
7
+ return /\s|-/.test(key);
8
+ }
9
+
10
+ export { needsQuotes };
@@ -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 { isKeyNeedsQuotes } from './isKeyNeedsQuotes.js';
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 = isKeyNeedsQuotes(key) ? `'${key}'` : key;
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 Request, type Source } from '../../../core/index.js';
2
- export declare function fetch(request?: Partial<Request>): Source;
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,EACL,KAAK,OAAO,EACZ,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,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"}
@@ -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
- function fetch(request) {
5
- // Defaults
6
- const normalizedRequest = {
7
- method: 'GET',
8
- ...request,
9
- };
10
- // Normalization
11
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
12
- // Reset fetch defaults
13
- const options = {
14
- method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
15
- };
16
- // Query
17
- const searchParams = new URLSearchParams(normalizedRequest.queryString
18
- ? arrayToObject(normalizedRequest.queryString)
19
- : undefined);
20
- const queryString = searchParams.size ? `?${searchParams.toString()}` : '';
21
- // Headers
22
- if (normalizedRequest.headers?.length) {
23
- options.headers = {};
24
- normalizedRequest.headers.forEach((header) => {
25
- options.headers[header.name] = header.value;
26
- });
27
- }
28
- // Cookies
29
- if (normalizedRequest.cookies?.length) {
30
- options.headers = options.headers || {};
31
- normalizedRequest.cookies.forEach((cookie) => {
32
- options.headers['Set-Cookie'] = options.headers['Set-Cookie']
33
- ? `${options.headers['Set-Cookie']}; ${cookie.name}=${cookie.value}`
34
- : `${cookie.name}=${cookie.value}`;
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
- // Add body
44
- if (normalizedRequest.postData) {
45
- // Plain text
46
- options.body = normalizedRequest.postData.text;
47
- // JSON
48
- if (normalizedRequest.postData.mimeType === 'application/json') {
49
- 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
+ }
50
59
  }
51
- }
52
- // Transform to JSON
53
- const jsonOptions = Object.keys(options).length
54
- ? `, ${objectToString(options)}`
55
- : '';
56
- // Code Template
57
- const code = `fetch('${normalizedRequest.url}${queryString}'${jsonOptions})`;
58
- return {
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 { 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,3 +1,6 @@
1
- import { type Request, type Source } from '../../../core/index.js';
2
- export declare function ofetch(request?: Partial<Request>): Source;
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,EACL,KAAK,OAAO,EACZ,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,wBAAgB,MAAM,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,MAAM,CAiFzD"}
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
- function ofetch(request) {
5
- // Defaults
6
- const normalizedRequest = {
7
- method: 'GET',
8
- ...request,
9
- };
10
- // Normalization
11
- normalizedRequest.method = normalizedRequest.method.toUpperCase();
12
- // Reset fetch defaults
13
- const options = {
14
- method: normalizedRequest.method === 'GET' ? undefined : normalizedRequest.method,
15
- };
16
- // Query
17
- const searchParams = new URLSearchParams(normalizedRequest.queryString
18
- ? arrayToObject(normalizedRequest.queryString)
19
- : undefined);
20
- if (searchParams.size) {
21
- options.query = {};
22
- searchParams.forEach((value, key) => {
23
- options.query[key] = value;
24
- });
25
- }
26
- // Headers
27
- if (normalizedRequest.headers?.length) {
28
- options.headers = {};
29
- normalizedRequest.headers.forEach((header) => {
30
- options.headers[header.name] = header.value;
31
- });
32
- }
33
- // Cookies
34
- if (normalizedRequest.cookies?.length) {
35
- options.headers = options.headers || {};
36
- normalizedRequest.cookies.forEach((cookie) => {
37
- options.headers['Set-Cookie'] = options.headers['Set-Cookie']
38
- ? `${options.headers['Set-Cookie']}; ${cookie.name}=${cookie.value}`
39
- : `${cookie.name}=${cookie.value}`;
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
- // Add body
49
- if (normalizedRequest.postData) {
50
- // Plain text
51
- options.body = normalizedRequest.postData.text;
52
- // JSON
53
- if (normalizedRequest.postData.mimeType === 'application/json') {
54
- options.body = JSON.parse(options.body);
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
- // Transform to JSON
58
- const jsonOptions = Object.keys(options).length
59
- ? `, ${objectToString(options)}`
60
- : '';
61
- // Code Template
62
- const code = `ofetch('${normalizedRequest.url}'${jsonOptions})`;
63
- return {
64
- target: 'js',
65
- client: 'ofetch',
66
- code,
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 { ofetch };
76
+ export { jsOfetch };
@@ -1,3 +1,6 @@
1
- import { type Request, type Source } from '../../../core/index.js';
2
- export declare function fetch(request?: Partial<Request>): Source;
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,EACL,KAAK,OAAO,EACZ,KAAK,MAAM,EAGZ,MAAM,eAAe,CAAA;AAEtB,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"}