@scalar/snippetz 0.1.6 → 0.2.2

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 (54) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +91 -0
  3. package/dist/core/index.d.ts +5 -0
  4. package/dist/core/index.d.ts.map +1 -0
  5. package/dist/core/index.js +3 -0
  6. package/dist/core/types.d.ts +12 -0
  7. package/dist/core/types.d.ts.map +1 -0
  8. package/dist/core/utils/arrayToObject.d.ts +3 -0
  9. package/dist/core/utils/arrayToObject.d.ts.map +1 -0
  10. package/dist/core/utils/arrayToObject.js +9 -0
  11. package/dist/core/utils/isKeyNeedsQuotes.d.ts +2 -0
  12. package/dist/core/utils/isKeyNeedsQuotes.d.ts.map +1 -0
  13. package/dist/core/utils/isKeyNeedsQuotes.js +5 -0
  14. package/dist/core/utils/objectToString.d.ts +2 -0
  15. package/dist/core/utils/objectToString.d.ts.map +1 -0
  16. package/dist/core/utils/objectToString.js +56 -0
  17. package/dist/index.d.ts +2 -1
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +1 -219
  20. package/dist/plugins/js/fetch/fetch.d.ts +3 -0
  21. package/dist/plugins/js/fetch/fetch.d.ts.map +1 -0
  22. package/dist/plugins/js/fetch/fetch.js +65 -0
  23. package/dist/plugins/js/fetch/index.d.ts +2 -0
  24. package/dist/plugins/js/fetch/index.d.ts.map +1 -0
  25. package/dist/plugins/js/fetch/index.js +1 -0
  26. package/dist/plugins/js/ofetch/index.d.ts +2 -0
  27. package/dist/plugins/js/ofetch/index.d.ts.map +1 -0
  28. package/dist/plugins/js/ofetch/index.js +1 -0
  29. package/dist/plugins/js/ofetch/ofetch.d.ts +3 -0
  30. package/dist/plugins/js/ofetch/ofetch.d.ts.map +1 -0
  31. package/dist/plugins/js/ofetch/ofetch.js +70 -0
  32. package/dist/plugins/node/fetch/fetch.d.ts +3 -0
  33. package/dist/plugins/node/fetch/fetch.d.ts.map +1 -0
  34. package/dist/plugins/node/fetch/fetch.js +65 -0
  35. package/dist/plugins/node/fetch/index.d.ts +2 -0
  36. package/dist/plugins/node/fetch/index.d.ts.map +1 -0
  37. package/dist/plugins/node/fetch/index.js +1 -0
  38. package/dist/plugins/node/ofetch/index.d.ts +2 -0
  39. package/dist/plugins/node/ofetch/index.d.ts.map +1 -0
  40. package/dist/plugins/node/ofetch/index.js +1 -0
  41. package/dist/plugins/node/ofetch/ofetch.d.ts +3 -0
  42. package/dist/plugins/node/ofetch/ofetch.d.ts.map +1 -0
  43. package/dist/plugins/node/ofetch/ofetch.js +70 -0
  44. package/dist/plugins/node/undici/index.d.ts +2 -0
  45. package/dist/plugins/node/undici/index.d.ts.map +1 -0
  46. package/dist/plugins/node/undici/index.js +1 -0
  47. package/dist/plugins/node/undici/undici.d.ts +3 -0
  48. package/dist/plugins/node/undici/undici.d.ts.map +1 -0
  49. package/dist/plugins/node/undici/undici.js +67 -0
  50. package/dist/snippetz.d.ts +4 -4
  51. package/dist/snippetz.d.ts.map +1 -1
  52. package/dist/snippetz.js +50 -0
  53. package/package.json +49 -23
  54. package/dist/index.umd.cjs +0 -8
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Scalar
3
+ Copyright (c) 2023-present Scalar
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md ADDED
@@ -0,0 +1,91 @@
1
+ # Snippetz
2
+
3
+ [![Version](https://img.shields.io/npm/v/%40scalar/snippetz)](https://www.npmjs.com/package/@scalar/snippetz)
4
+ [![Downloads](https://img.shields.io/npm/dm/%40scalar/snippetz)](https://www.npmjs.com/package/@scalar/snippetz)
5
+ [![License](https://img.shields.io/npm/l/%40scalar%2Fsnippetz)](https://www.npmjs.com/package/@scalar/snippetz)
6
+ [![Discord](https://img.shields.io/discord/1135330207960678410?style=flat&color=5865F2)](https://discord.gg/scalar)
7
+
8
+ A modern way to generate HTTP request examples for different languages and libraries.
9
+
10
+ ## Installation
11
+
12
+ ```bash
13
+ npm install @scalar/snippetz
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import { snippetz } from '@scalar/snippetz'
20
+
21
+ const snippet = snippetz().print('node', 'undici', {
22
+ url: 'https://example.com',
23
+ })
24
+
25
+ /* Output */
26
+
27
+ // import { request } from 'undici'
28
+ //
29
+ // const { statusCode, body } = await request(
30
+ // 'https://example.com',
31
+ // )
32
+ ```
33
+
34
+ ## API
35
+
36
+ ### Get all plugins
37
+
38
+ ```ts
39
+ import { snippetz } from '@scalar/snippetz'
40
+
41
+ const snippet = snippetz().plugins()
42
+
43
+ /* Output */
44
+
45
+ // [
46
+ // {
47
+ // target: 'node',
48
+ // client: 'undici',
49
+ // }
50
+ // ]
51
+ ```
52
+
53
+ ### Check if a plugin is loaded
54
+
55
+ ```ts
56
+ import { snippetz } from '@scalar/snippetz'
57
+
58
+ const snippet = snippetz().hasPlugin('node', 'undici')
59
+
60
+ /* Output */
61
+
62
+ // true
63
+ ```
64
+
65
+ ### Lean usage
66
+
67
+ You can also just use one specific plugin to keep your bundle size small.
68
+
69
+ ```ts
70
+ import { undici } from '@scalar/snippetz/plugins/node/undici'
71
+
72
+ const source = undici({
73
+ url: 'https://example.com',
74
+ })
75
+
76
+ console.log(source.code)
77
+
78
+ // import { request } from 'undici'
79
+
80
+ // const { statusCode, body } = await request(
81
+ // 'url': 'https://example.com',
82
+ // )
83
+ ```
84
+
85
+ ## Community
86
+
87
+ We are API nerds. You too? Let’s chat on Discord: <https://discord.gg/scalar>
88
+
89
+ ## License
90
+
91
+ The source code in this repository is licensed under [MIT](https://github.com/scalar/scalar/blob/main/LICENSE).
@@ -0,0 +1,5 @@
1
+ export * from './utils/arrayToObject.js';
2
+ export * from './utils/isKeyNeedsQuotes.js';
3
+ export * from './utils/objectToString.js';
4
+ export * from './types';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,3 @@
1
+ export { arrayToObject } from './utils/arrayToObject.js';
2
+ export { isKeyNeedsQuotes } from './utils/isKeyNeedsQuotes.js';
3
+ export { objectToString } from './utils/objectToString.js';
@@ -0,0 +1,12 @@
1
+ export type { Request } from 'har-format';
2
+ export type Source = {
3
+ /** The language or environment. */
4
+ target: TargetId;
5
+ /** The identifier of the client. */
6
+ client: ClientId;
7
+ /** The actual source code. */
8
+ code: string;
9
+ };
10
+ export type TargetId = 'node' | 'js';
11
+ export type ClientId = 'undici' | 'fetch' | 'ofetch';
12
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,3 @@
1
+ /** Helper function to map { name: 'foo', value: 'bar' } to { foo: 'bar' } */
2
+ export declare function arrayToObject(items: any): any;
3
+ //# sourceMappingURL=arrayToObject.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,9 @@
1
+ /** Helper function to map { name: 'foo', value: 'bar' } to { foo: 'bar' } */
2
+ function arrayToObject(items) {
3
+ return items.reduce((acc, item) => {
4
+ acc[item.name] = item.value;
5
+ return acc;
6
+ }, {});
7
+ }
8
+
9
+ export { arrayToObject };
@@ -0,0 +1,2 @@
1
+ export declare function isKeyNeedsQuotes(key: string): boolean;
2
+ //# sourceMappingURL=isKeyNeedsQuotes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isKeyNeedsQuotes.d.ts","sourceRoot":"","sources":["../../../src/core/utils/isKeyNeedsQuotes.ts"],"names":[],"mappings":"AAAA,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,WAE3C"}
@@ -0,0 +1,5 @@
1
+ function isKeyNeedsQuotes(key) {
2
+ return /\s|-/.test(key);
3
+ }
4
+
5
+ export { isKeyNeedsQuotes };
@@ -0,0 +1,2 @@
1
+ export declare function objectToString(obj: Record<string, any>, indent?: number): string;
2
+ //# sourceMappingURL=objectToString.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,56 @@
1
+ import { isKeyNeedsQuotes } from './isKeyNeedsQuotes.js';
2
+
3
+ function objectToString(obj, indent = 0) {
4
+ const parts = [];
5
+ const indentation = ' '.repeat(indent);
6
+ const innerIndentation = ' '.repeat(indent + 2);
7
+ for (const [key, value] of Object.entries(obj)) {
8
+ const formattedKey = isKeyNeedsQuotes(key) ? `'${key}'` : key;
9
+ if (Array.isArray(value)) {
10
+ const arrayString = value
11
+ .map((item) => {
12
+ if (typeof item === 'string') {
13
+ return `'${item}'`;
14
+ }
15
+ else if (item && typeof item === 'object') {
16
+ return objectToString(item, indent + 2);
17
+ }
18
+ else {
19
+ return item;
20
+ }
21
+ })
22
+ .join(`, ${innerIndentation}`);
23
+ parts.push(`${innerIndentation}${formattedKey}: [${arrayString}]`);
24
+ }
25
+ else if (value && typeof value === 'object') {
26
+ parts.push(`${innerIndentation}${formattedKey}: ${objectToString(value, indent + 2)}`);
27
+ }
28
+ else if (typeof value === 'string') {
29
+ let formattedValue = `${value}`;
30
+ if (value.startsWith('JSON.stringify')) {
31
+ // If it has more than one line, add indentation to the other lines
32
+ const lines = value.split('\n');
33
+ if (lines.length > 1) {
34
+ formattedValue = lines
35
+ .map((line, index) => {
36
+ if (index === 0) {
37
+ return line;
38
+ }
39
+ return `${innerIndentation}${line}`;
40
+ })
41
+ .join('\n');
42
+ }
43
+ }
44
+ else {
45
+ formattedValue = `'${value}'`;
46
+ }
47
+ parts.push(`${innerIndentation}${formattedKey}: ${formattedValue}`);
48
+ }
49
+ else {
50
+ parts.push(`${innerIndentation}${formattedKey}: ${value}`);
51
+ }
52
+ }
53
+ return `{\n${parts.join(',\n')}\n${indentation}}`;
54
+ }
55
+
56
+ export { objectToString };
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export * from './snippetz';
1
+ export * from './snippetz.js';
2
+ export * from './core/types';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAA;AAC1B,cAAc,cAAc,CAAA"}
package/dist/index.js CHANGED
@@ -1,219 +1 @@
1
- function u(n) {
2
- return n.reduce((t, e) => (t[e.name] = e.value, t), {});
3
- }
4
- function p(n) {
5
- return /\s|-/.test(n);
6
- }
7
- function c(n, t = 0) {
8
- let e = [], a = " ".repeat(t), s = " ".repeat(t + 2);
9
- for (const [h, i] of Object.entries(n)) {
10
- let d = p(h) ? `'${h}'` : h;
11
- if (Array.isArray(i)) {
12
- const r = i.map((o) => typeof o == "string" ? `'${o}'` : o && typeof o == "object" ? c(o, t + 2) : o).join(`, ${s}`);
13
- e.push(`${s}${d}: [${r}]`);
14
- } else if (i && typeof i == "object")
15
- e.push(
16
- `${s}${d}: ${c(
17
- i,
18
- t + 2
19
- )}`
20
- );
21
- else if (typeof i == "string") {
22
- let r = `${i}`;
23
- if (i.startsWith("JSON.stringify")) {
24
- const o = i.split(`
25
- `);
26
- o.length > 1 && (r = o.map((f, l) => l === 0 ? f : `${s}${f}`).join(`
27
- `));
28
- } else
29
- r = `'${i}'`;
30
- e.push(`${s}${d}: ${r}`);
31
- } else
32
- e.push(`${s}${d}: ${i}`);
33
- }
34
- return `{
35
- ${e.join(`,
36
- `)}
37
- ${a}}`;
38
- }
39
- function m(n) {
40
- var d, r;
41
- const t = {
42
- method: "GET",
43
- ...n
44
- };
45
- t.method = t.method.toUpperCase();
46
- const e = {
47
- method: t.method === "GET" ? void 0 : t.method
48
- }, a = new URLSearchParams(
49
- t.queryString ? u(t.queryString) : void 0
50
- ), s = a.size ? `?${a.toString()}` : "";
51
- (d = t.headers) != null && d.length && (e.headers = {}, t.headers.forEach((o) => {
52
- e.headers[o.name] = o.value;
53
- })), (r = t.cookies) != null && r.length && (e.headers = e.headers || {}, t.cookies.forEach((o) => {
54
- e.headers["Set-Cookie"] = e.headers["Set-Cookie"] ? `${e.headers["Set-Cookie"]}; ${o.name}=${o.value}` : `${o.name}=${o.value}`;
55
- })), Object.keys(e).forEach((o) => {
56
- e[o] === void 0 && delete e[o];
57
- }), t.postData && (e.body = t.postData.text, t.postData.mimeType === "application/json" && (e.body = `JSON.stringify(${c(JSON.parse(e.body))})`));
58
- const h = Object.keys(e).length ? `, ${c(e)}` : "";
59
- return {
60
- target: "node",
61
- client: "undici",
62
- code: `import { request } from 'undici'
63
-
64
- const { statusCode, body } = await request('${t.url}${s}'${h})`
65
- };
66
- }
67
- function $(n) {
68
- var d, r;
69
- const t = {
70
- method: "GET",
71
- ...n
72
- };
73
- t.method = t.method.toUpperCase();
74
- const e = {
75
- method: t.method === "GET" ? void 0 : t.method
76
- }, a = new URLSearchParams(
77
- t.queryString ? u(t.queryString) : void 0
78
- ), s = a.size ? `?${a.toString()}` : "";
79
- (d = t.headers) != null && d.length && (e.headers = {}, t.headers.forEach((o) => {
80
- e.headers[o.name] = o.value;
81
- })), (r = t.cookies) != null && r.length && (e.headers = e.headers || {}, t.cookies.forEach((o) => {
82
- e.headers["Set-Cookie"] = e.headers["Set-Cookie"] ? `${e.headers["Set-Cookie"]}; ${o.name}=${o.value}` : `${o.name}=${o.value}`;
83
- })), Object.keys(e).forEach((o) => {
84
- e[o] === void 0 && delete e[o];
85
- }), t.postData && (e.body = t.postData.text, t.postData.mimeType === "application/json" && (e.body = `JSON.stringify(${c(
86
- JSON.parse(e.body)
87
- )})`));
88
- const h = Object.keys(e).length ? `, ${c(e)}` : "";
89
- return {
90
- target: "node",
91
- client: "fetch",
92
- code: `fetch('${t.url}${s}'${h})`
93
- };
94
- }
95
- function g(n) {
96
- var d, r;
97
- const t = {
98
- method: "GET",
99
- ...n
100
- };
101
- t.method = t.method.toUpperCase();
102
- const e = {
103
- method: t.method === "GET" ? void 0 : t.method
104
- }, a = new URLSearchParams(
105
- t.queryString ? u(t.queryString) : void 0
106
- ), s = a.size ? `?${a.toString()}` : "";
107
- (d = t.headers) != null && d.length && (e.headers = {}, t.headers.forEach((o) => {
108
- e.headers[o.name] = o.value;
109
- })), (r = t.cookies) != null && r.length && (e.headers = e.headers || {}, t.cookies.forEach((o) => {
110
- e.headers["Set-Cookie"] = e.headers["Set-Cookie"] ? `${e.headers["Set-Cookie"]}; ${o.name}=${o.value}` : `${o.name}=${o.value}`;
111
- })), Object.keys(e).forEach((o) => {
112
- e[o] === void 0 && delete e[o];
113
- }), t.postData && (e.body = t.postData.text, t.postData.mimeType === "application/json" && (e.body = `JSON.stringify(${c(
114
- JSON.parse(e.body)
115
- )})`));
116
- const h = Object.keys(e).length ? `, ${c(e)}` : "";
117
- return {
118
- target: "js",
119
- client: "fetch",
120
- code: `fetch('${t.url}${s}'${h})`
121
- };
122
- }
123
- function y(n) {
124
- var i, d;
125
- const t = {
126
- method: "GET",
127
- ...n
128
- };
129
- t.method = t.method.toUpperCase();
130
- const e = {
131
- method: t.method === "GET" ? void 0 : t.method
132
- }, a = new URLSearchParams(
133
- t.queryString ? u(t.queryString) : void 0
134
- );
135
- a.size && (e.query = {}, a.forEach((r, o) => {
136
- e.query[o] = r;
137
- })), (i = t.headers) != null && i.length && (e.headers = {}, t.headers.forEach((r) => {
138
- e.headers[r.name] = r.value;
139
- })), (d = t.cookies) != null && d.length && (e.headers = e.headers || {}, t.cookies.forEach((r) => {
140
- e.headers["Set-Cookie"] = e.headers["Set-Cookie"] ? `${e.headers["Set-Cookie"]}; ${r.name}=${r.value}` : `${r.name}=${r.value}`;
141
- })), Object.keys(e).forEach((r) => {
142
- e[r] === void 0 && delete e[r];
143
- }), t.postData && (e.body = t.postData.text, t.postData.mimeType === "application/json" && (e.body = JSON.parse(e.body)));
144
- const s = Object.keys(e).length ? `, ${c(e)}` : "";
145
- return {
146
- target: "js",
147
- client: "ofetch",
148
- code: `ofetch('${t.url}'${s})`
149
- };
150
- }
151
- function S(n) {
152
- var i, d;
153
- const t = {
154
- method: "GET",
155
- ...n
156
- };
157
- t.method = t.method.toUpperCase();
158
- const e = {
159
- method: t.method === "GET" ? void 0 : t.method
160
- }, a = new URLSearchParams(
161
- t.queryString ? u(t.queryString) : void 0
162
- );
163
- a.size && (e.query = {}, a.forEach((r, o) => {
164
- e.query[o] = r;
165
- })), (i = t.headers) != null && i.length && (e.headers = {}, t.headers.forEach((r) => {
166
- e.headers[r.name] = r.value;
167
- })), (d = t.cookies) != null && d.length && (e.headers = e.headers || {}, t.cookies.forEach((r) => {
168
- e.headers["Set-Cookie"] = e.headers["Set-Cookie"] ? `${e.headers["Set-Cookie"]}; ${r.name}=${r.value}` : `${r.name}=${r.value}`;
169
- })), Object.keys(e).forEach((r) => {
170
- e[r] === void 0 && delete e[r];
171
- }), t.postData && (e.body = t.postData.text, t.postData.mimeType === "application/json" && (e.body = JSON.parse(e.body)));
172
- const s = Object.keys(e).length ? `, ${c(e)}` : "";
173
- return {
174
- target: "node",
175
- client: "ofetch",
176
- code: `ofetch('${t.url}'${s})`
177
- };
178
- }
179
- function v() {
180
- const n = [m, $, g, y, S];
181
- return {
182
- get(t, e, a) {
183
- const s = this.findPlugin(t, e);
184
- if (s)
185
- return s(a);
186
- },
187
- print(t, e, a) {
188
- var s;
189
- return (s = this.get(t, e, a)) == null ? void 0 : s.code;
190
- },
191
- targets() {
192
- return n.map((t) => t().target).filter((t, e, a) => a.indexOf(t) === e);
193
- },
194
- clients() {
195
- return n.map((t) => t().client);
196
- },
197
- plugins() {
198
- return n.map((t) => {
199
- const e = t();
200
- return {
201
- target: e.target,
202
- client: e.client
203
- };
204
- });
205
- },
206
- findPlugin(t, e) {
207
- return n.find((a) => {
208
- const s = a();
209
- return s.target === t && s.client === e;
210
- });
211
- },
212
- hasPlugin(t, e) {
213
- return !!this.findPlugin(t, e);
214
- }
215
- };
216
- }
217
- export {
218
- v as snippetz
219
- };
1
+ export { snippetz } from './snippetz.js';
@@ -0,0 +1,3 @@
1
+ import { type Request, type Source } from '../../../core/index.js';
2
+ export declare function fetch(request?: Partial<Request>): Source;
3
+ //# sourceMappingURL=fetch.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,65 @@
1
+ import { arrayToObject } from '../../../core/utils/arrayToObject.js';
2
+ import { objectToString } from '../../../core/utils/objectToString.js';
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];
41
+ }
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))})`;
50
+ }
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
+ }
64
+
65
+ export { fetch };
@@ -0,0 +1,2 @@
1
+ export * from './fetch.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/fetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA"}
@@ -0,0 +1 @@
1
+ export { fetch } from './fetch.js';
@@ -0,0 +1,2 @@
1
+ export * from './ofetch.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/plugins/js/ofetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA"}
@@ -0,0 +1 @@
1
+ export { ofetch } from './ofetch.js';
@@ -0,0 +1,3 @@
1
+ import { type Request, type Source } from '../../../core/index.js';
2
+ export declare function ofetch(request?: Partial<Request>): Source;
3
+ //# sourceMappingURL=ofetch.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,70 @@
1
+ import { arrayToObject } from '../../../core/utils/arrayToObject.js';
2
+ import { objectToString } from '../../../core/utils/objectToString.js';
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];
46
+ }
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);
55
+ }
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
+ }
69
+
70
+ export { ofetch };
@@ -0,0 +1,3 @@
1
+ import { type Request, type Source } from '../../../core/index.js';
2
+ export declare function fetch(request?: Partial<Request>): Source;
3
+ //# sourceMappingURL=fetch.d.ts.map
@@ -0,0 +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"}
@@ -0,0 +1,65 @@
1
+ import { arrayToObject } from '../../../core/utils/arrayToObject.js';
2
+ import { objectToString } from '../../../core/utils/objectToString.js';
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];
41
+ }
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))})`;
50
+ }
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: 'node',
60
+ client: 'fetch',
61
+ code,
62
+ };
63
+ }
64
+
65
+ export { fetch };
@@ -0,0 +1,2 @@
1
+ export * from './fetch.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/fetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA"}
@@ -0,0 +1 @@
1
+ export { fetch } from './fetch.js';
@@ -0,0 +1,2 @@
1
+ export * from './ofetch.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/ofetch/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA"}
@@ -0,0 +1 @@
1
+ export { ofetch } from './ofetch.js';
@@ -0,0 +1,3 @@
1
+ import { type Request, type Source } from '../../../core/index.js';
2
+ export declare function ofetch(request?: Partial<Request>): Source;
3
+ //# sourceMappingURL=ofetch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ofetch.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/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"}
@@ -0,0 +1,70 @@
1
+ import { arrayToObject } from '../../../core/utils/arrayToObject.js';
2
+ import { objectToString } from '../../../core/utils/objectToString.js';
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];
46
+ }
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);
55
+ }
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: 'node',
65
+ client: 'ofetch',
66
+ code,
67
+ };
68
+ }
69
+
70
+ export { ofetch };
@@ -0,0 +1,2 @@
1
+ export * from './undici.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/undici/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA"}
@@ -0,0 +1 @@
1
+ export { undici } from './undici.js';
@@ -0,0 +1,3 @@
1
+ import { type Request, type Source } from '../../../core/index.js';
2
+ export declare function undici(request?: Partial<Request>): Source;
3
+ //# sourceMappingURL=undici.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"undici.d.ts","sourceRoot":"","sources":["../../../../src/plugins/node/undici/undici.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,CA+EzD"}
@@ -0,0 +1,67 @@
1
+ import { arrayToObject } from '../../../core/utils/arrayToObject.js';
2
+ import { objectToString } from '../../../core/utils/objectToString.js';
3
+
4
+ function undici(request) {
5
+ // Defaults
6
+ const normalizedRequest = {
7
+ method: 'GET',
8
+ ...request,
9
+ };
10
+ // Normalization
11
+ normalizedRequest.method = normalizedRequest.method.toUpperCase();
12
+ // Reset undici 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];
41
+ }
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))})`;
50
+ }
51
+ }
52
+ // Transform to JSON
53
+ const jsonOptions = Object.keys(options).length
54
+ ? `, ${objectToString(options)}`
55
+ : '';
56
+ // Code Template
57
+ const code = `import { request } from 'undici'
58
+
59
+ const { statusCode, body } = await request('${normalizedRequest.url}${queryString}'${jsonOptions})`;
60
+ return {
61
+ target: 'node',
62
+ client: 'undici',
63
+ code,
64
+ };
65
+ }
66
+
67
+ export { undici };
@@ -1,7 +1,7 @@
1
- import type { TargetId, ClientId, Request } from '@scalar/snippetz-core';
2
- import { undici } from '@scalar/snippetz-plugin-node-undici';
1
+ import type { ClientId, Request, TargetId } from './core/index.js';
2
+ import { undici } from './plugins/node/undici/index.js';
3
3
  export declare function snippetz(): {
4
- get(target: TargetId, client: ClientId, request: Partial<Request>): import("@scalar/snippetz-core").Source | undefined;
4
+ get(target: TargetId, client: ClientId, request: Partial<Request>): import("./core/index.js").Source | undefined;
5
5
  print(target: TargetId, client: ClientId, request: Partial<Request>): string | undefined;
6
6
  targets(): TargetId[];
7
7
  clients(): ClientId[];
@@ -10,6 +10,6 @@ export declare function snippetz(): {
10
10
  client: ClientId;
11
11
  }[];
12
12
  findPlugin(target: TargetId, client: ClientId): typeof undici | undefined;
13
- hasPlugin(target: string, client: ClientId): boolean;
13
+ hasPlugin(target: string, client: string): boolean;
14
14
  };
15
15
  //# 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,QAAQ,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AACxE,OAAO,EAAE,MAAM,EAAE,MAAM,qCAAqC,CAAA;AAM5D,wBAAgB,QAAQ;gBAIR,QAAQ,UAAU,QAAQ,WAAW,QAAQ,OAAO,CAAC;kBAOnD,QAAQ,UAAU,QAAQ,WAAW,QAAQ,OAAO,CAAC;;;;;;;uBAyBhD,QAAQ,UAAU,QAAQ;sBAO3B,MAAM,UAAU,QAAQ;EAI7C"}
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;AAKzD,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAE9C,wBAAgB,QAAQ;gBAIR,QAAQ,UAAU,QAAQ,WAAW,OAAO,CAAC,OAAO,CAAC;kBAOnD,QAAQ,UAAU,QAAQ,WAAW,OAAO,CAAC,OAAO,CAAC;;;;;;;uBAyBhD,QAAQ,UAAU,QAAQ;sBAO3B,MAAM,UAAU,MAAM;EAI3C"}
@@ -0,0 +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
+
7
+ function snippetz() {
8
+ const plugins = [undici, fetch, fetch$1, ofetch, ofetch$1];
9
+ return {
10
+ get(target, client, request) {
11
+ const plugin = this.findPlugin(target, client);
12
+ if (plugin) {
13
+ return plugin(request);
14
+ }
15
+ },
16
+ print(target, client, request) {
17
+ return this.get(target, client, request)?.code;
18
+ },
19
+ targets() {
20
+ return (plugins
21
+ // all targets
22
+ .map((plugin) => plugin().target)
23
+ // unique values
24
+ .filter((value, index, self) => self.indexOf(value) === index));
25
+ },
26
+ clients() {
27
+ return plugins.map((plugin) => plugin().client);
28
+ },
29
+ plugins() {
30
+ return plugins.map((plugin) => {
31
+ const details = plugin();
32
+ return {
33
+ target: details.target,
34
+ client: details.client,
35
+ };
36
+ });
37
+ },
38
+ findPlugin(target, client) {
39
+ return plugins.find((plugin) => {
40
+ const details = plugin();
41
+ return details.target === target && details.client === client;
42
+ });
43
+ },
44
+ hasPlugin(target, client) {
45
+ return Boolean(this.findPlugin(target, client));
46
+ },
47
+ };
48
+ }
49
+
50
+ export { snippetz };
package/package.json CHANGED
@@ -1,37 +1,63 @@
1
1
  {
2
2
  "name": "@scalar/snippetz",
3
- "version": "0.1.6",
4
- "type": "module",
5
- "devDependencies": {
6
- "@vitest/ui": "^1.0.4",
7
- "typescript": "^5.2.2",
8
- "vite": "^5.0.0",
9
- "vitest": "^1.0.4"
3
+ "license": "MIT",
4
+ "author": "Scalar (https://github.com/scalar)",
5
+ "homepage": "https://github.com/scalar/scalar",
6
+ "bugs": "https://github.com/scalar/scalar/issues/new/choose",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/scalar/scalar.git",
10
+ "directory": "packages/snippetz"
10
11
  },
11
- "dependencies": {
12
- "@scalar/snippetz-core": "0.1.4",
13
- "@scalar/snippetz-plugin-node-fetch": "0.1.2",
14
- "@scalar/snippetz-plugin-node-ofetch": "^0.1.1",
15
- "@scalar/snippetz-plugin-node-undici": "0.1.6",
16
- "@scalar/snippetz-plugin-js-ofetch": "^0.1.1",
17
- "@scalar/snippetz-plugin-js-fetch": "0.1.1"
12
+ "version": "0.2.2",
13
+ "engines": {
14
+ "node": ">=18"
18
15
  },
19
- "files": [
20
- "dist"
21
- ],
16
+ "type": "module",
22
17
  "types": "./dist/index.d.ts",
23
- "main": "./dist/index.umd.cjs",
24
- "module": "./dist/index.js",
25
18
  "exports": {
26
19
  ".": {
27
20
  "import": "./dist/index.js",
28
- "require": "./dist/index.umd.cjs"
21
+ "types": "./dist/index.d.ts"
22
+ },
23
+ "./plugins/node/undici": {
24
+ "import": "./dist/plugins/node/undici/index.js",
25
+ "types": "./dist/plugins/node/undici/index.d.ts"
26
+ },
27
+ "./plugins/node/ofetch": {
28
+ "import": "./dist/plugins/node/ofetch/index.js",
29
+ "types": "./dist/plugins/node/ofetch/index.d.ts"
30
+ },
31
+ "./plugins/node/fetch": {
32
+ "import": "./dist/plugins/node/fetch/index.js",
33
+ "types": "./dist/plugins/node/fetch/index.d.ts"
34
+ },
35
+ "./plugins/js/ofetch": {
36
+ "import": "./dist/plugins/js/ofetch/index.js",
37
+ "types": "./dist/plugins/js/ofetch/index.d.ts"
38
+ },
39
+ "./plugins/js/fetch": {
40
+ "import": "./dist/plugins/js/fetch/index.js",
41
+ "types": "./dist/plugins/js/fetch/index.d.ts"
42
+ },
43
+ "./core": {
44
+ "import": "./dist/core/index.js",
45
+ "types": "./dist/core/index.d.ts"
29
46
  }
30
47
  },
48
+ "files": [
49
+ "dist",
50
+ "CHANGELOG"
51
+ ],
52
+ "module": "./dist/index.js",
53
+ "devDependencies": {
54
+ "@types/har-format": "^1.2.15",
55
+ "@scalar/build-tooling": "0.1.10"
56
+ },
31
57
  "scripts": {
32
- "build": "vite build && tsc -p tsconfig.build.json",
58
+ "build": "scalar-build-rollup",
33
59
  "test": "vitest",
34
- "test:ui": "vitest --ui",
35
- "types:check": "tsc --noEmit --skipLibCheck"
60
+ "types:build": "scalar-types-build",
61
+ "types:check": "scalar-types-check"
36
62
  }
37
63
  }
@@ -1,8 +0,0 @@
1
- (function(f,u){typeof exports=="object"&&typeof module<"u"?u(exports):typeof define=="function"&&define.amd?define(["exports"],u):(f=typeof globalThis<"u"?globalThis:f||self,u(f["@scalar/snippetz"]={}))})(this,function(f){"use strict";function u(a){return a.reduce((t,e)=>(t[e.name]=e.value,t),{})}function p(a){return/\s|-/.test(a)}function c(a,t=0){let e=[],r=" ".repeat(t),n=" ".repeat(t+2);for(const[h,i]of Object.entries(a)){let d=p(h)?`'${h}'`:h;if(Array.isArray(i)){const s=i.map(o=>typeof o=="string"?`'${o}'`:o&&typeof o=="object"?c(o,t+2):o).join(`, ${n}`);e.push(`${n}${d}: [${s}]`)}else if(i&&typeof i=="object")e.push(`${n}${d}: ${c(i,t+2)}`);else if(typeof i=="string"){let s=`${i}`;if(i.startsWith("JSON.stringify")){const o=i.split(`
2
- `);o.length>1&&(s=o.map((l,v)=>v===0?l:`${n}${l}`).join(`
3
- `))}else s=`'${i}'`;e.push(`${n}${d}: ${s}`)}else e.push(`${n}${d}: ${i}`)}return`{
4
- ${e.join(`,
5
- `)}
6
- ${r}}`}function m(a){var d,s;const t={method:"GET",...a};t.method=t.method.toUpperCase();const e={method:t.method==="GET"?void 0:t.method},r=new URLSearchParams(t.queryString?u(t.queryString):void 0),n=r.size?`?${r.toString()}`:"";(d=t.headers)!=null&&d.length&&(e.headers={},t.headers.forEach(o=>{e.headers[o.name]=o.value})),(s=t.cookies)!=null&&s.length&&(e.headers=e.headers||{},t.cookies.forEach(o=>{e.headers["Set-Cookie"]=e.headers["Set-Cookie"]?`${e.headers["Set-Cookie"]}; ${o.name}=${o.value}`:`${o.name}=${o.value}`})),Object.keys(e).forEach(o=>{e[o]===void 0&&delete e[o]}),t.postData&&(e.body=t.postData.text,t.postData.mimeType==="application/json"&&(e.body=`JSON.stringify(${c(JSON.parse(e.body))})`));const h=Object.keys(e).length?`, ${c(e)}`:"";return{target:"node",client:"undici",code:`import { request } from 'undici'
7
-
8
- const { statusCode, body } = await request('${t.url}${n}'${h})`}}function $(a){var d,s;const t={method:"GET",...a};t.method=t.method.toUpperCase();const e={method:t.method==="GET"?void 0:t.method},r=new URLSearchParams(t.queryString?u(t.queryString):void 0),n=r.size?`?${r.toString()}`:"";(d=t.headers)!=null&&d.length&&(e.headers={},t.headers.forEach(o=>{e.headers[o.name]=o.value})),(s=t.cookies)!=null&&s.length&&(e.headers=e.headers||{},t.cookies.forEach(o=>{e.headers["Set-Cookie"]=e.headers["Set-Cookie"]?`${e.headers["Set-Cookie"]}; ${o.name}=${o.value}`:`${o.name}=${o.value}`})),Object.keys(e).forEach(o=>{e[o]===void 0&&delete e[o]}),t.postData&&(e.body=t.postData.text,t.postData.mimeType==="application/json"&&(e.body=`JSON.stringify(${c(JSON.parse(e.body))})`));const h=Object.keys(e).length?`, ${c(e)}`:"";return{target:"node",client:"fetch",code:`fetch('${t.url}${n}'${h})`}}function y(a){var d,s;const t={method:"GET",...a};t.method=t.method.toUpperCase();const e={method:t.method==="GET"?void 0:t.method},r=new URLSearchParams(t.queryString?u(t.queryString):void 0),n=r.size?`?${r.toString()}`:"";(d=t.headers)!=null&&d.length&&(e.headers={},t.headers.forEach(o=>{e.headers[o.name]=o.value})),(s=t.cookies)!=null&&s.length&&(e.headers=e.headers||{},t.cookies.forEach(o=>{e.headers["Set-Cookie"]=e.headers["Set-Cookie"]?`${e.headers["Set-Cookie"]}; ${o.name}=${o.value}`:`${o.name}=${o.value}`})),Object.keys(e).forEach(o=>{e[o]===void 0&&delete e[o]}),t.postData&&(e.body=t.postData.text,t.postData.mimeType==="application/json"&&(e.body=`JSON.stringify(${c(JSON.parse(e.body))})`));const h=Object.keys(e).length?`, ${c(e)}`:"";return{target:"js",client:"fetch",code:`fetch('${t.url}${n}'${h})`}}function g(a){var i,d;const t={method:"GET",...a};t.method=t.method.toUpperCase();const e={method:t.method==="GET"?void 0:t.method},r=new URLSearchParams(t.queryString?u(t.queryString):void 0);r.size&&(e.query={},r.forEach((s,o)=>{e.query[o]=s})),(i=t.headers)!=null&&i.length&&(e.headers={},t.headers.forEach(s=>{e.headers[s.name]=s.value})),(d=t.cookies)!=null&&d.length&&(e.headers=e.headers||{},t.cookies.forEach(s=>{e.headers["Set-Cookie"]=e.headers["Set-Cookie"]?`${e.headers["Set-Cookie"]}; ${s.name}=${s.value}`:`${s.name}=${s.value}`})),Object.keys(e).forEach(s=>{e[s]===void 0&&delete e[s]}),t.postData&&(e.body=t.postData.text,t.postData.mimeType==="application/json"&&(e.body=JSON.parse(e.body)));const n=Object.keys(e).length?`, ${c(e)}`:"";return{target:"js",client:"ofetch",code:`ofetch('${t.url}'${n})`}}function S(a){var i,d;const t={method:"GET",...a};t.method=t.method.toUpperCase();const e={method:t.method==="GET"?void 0:t.method},r=new URLSearchParams(t.queryString?u(t.queryString):void 0);r.size&&(e.query={},r.forEach((s,o)=>{e.query[o]=s})),(i=t.headers)!=null&&i.length&&(e.headers={},t.headers.forEach(s=>{e.headers[s.name]=s.value})),(d=t.cookies)!=null&&d.length&&(e.headers=e.headers||{},t.cookies.forEach(s=>{e.headers["Set-Cookie"]=e.headers["Set-Cookie"]?`${e.headers["Set-Cookie"]}; ${s.name}=${s.value}`:`${s.name}=${s.value}`})),Object.keys(e).forEach(s=>{e[s]===void 0&&delete e[s]}),t.postData&&(e.body=t.postData.text,t.postData.mimeType==="application/json"&&(e.body=JSON.parse(e.body)));const n=Object.keys(e).length?`, ${c(e)}`:"";return{target:"node",client:"ofetch",code:`ofetch('${t.url}'${n})`}}function b(){const a=[m,$,y,g,S];return{get(t,e,r){const n=this.findPlugin(t,e);if(n)return n(r)},print(t,e,r){var n;return(n=this.get(t,e,r))==null?void 0:n.code},targets(){return a.map(t=>t().target).filter((t,e,r)=>r.indexOf(t)===e)},clients(){return a.map(t=>t().client)},plugins(){return a.map(t=>{const e=t();return{target:e.target,client:e.client}})},findPlugin(t,e){return a.find(r=>{const n=r();return n.target===t&&n.client===e})},hasPlugin(t,e){return!!this.findPlugin(t,e)}}}f.snippetz=b,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})});