@scalar/snippetz 0.2.0 → 0.2.3

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/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).
@@ -1,11 +1,11 @@
1
1
  import { isKeyNeedsQuotes } from './isKeyNeedsQuotes.js';
2
2
 
3
3
  function objectToString(obj, indent = 0) {
4
- let parts = [];
5
- let indentation = ' '.repeat(indent);
6
- let innerIndentation = ' '.repeat(indent + 2);
4
+ const parts = [];
5
+ const indentation = ' '.repeat(indent);
6
+ const innerIndentation = ' '.repeat(indent + 2);
7
7
  for (const [key, value] of Object.entries(obj)) {
8
- let formattedKey = isKeyNeedsQuotes(key) ? `'${key}'` : key;
8
+ const formattedKey = isKeyNeedsQuotes(key) ? `'${key}'` : key;
9
9
  if (Array.isArray(value)) {
10
10
  const arrayString = value
11
11
  .map((item) => {
@@ -1,3 +1,3 @@
1
- import { type Source, type Request } from '../../../core/index.js';
1
+ import { type Request, type Source } from '../../../core/index.js';
2
2
  export declare function fetch(request?: Partial<Request>): Source;
3
3
  //# 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,MAAM,EACX,KAAK,OAAO,EAGb,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,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,3 +1,3 @@
1
- import { type Source, type Request } from '../../../core/index.js';
1
+ import { type Request, type Source } from '../../../core/index.js';
2
2
  export declare function ofetch(request?: Partial<Request>): Source;
3
3
  //# 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,MAAM,EACX,KAAK,OAAO,EAGb,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,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,3 +1,3 @@
1
- import { type Source, type Request } from '../../../core/index.js';
1
+ import { type Request, type Source } from '../../../core/index.js';
2
2
  export declare function fetch(request?: Partial<Request>): Source;
3
3
  //# 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,MAAM,EACX,KAAK,OAAO,EAGb,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,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,3 +1,3 @@
1
- import { type Source, type Request } from '../../../core/index.js';
1
+ import { type Request, type Source } from '../../../core/index.js';
2
2
  export declare function ofetch(request?: Partial<Request>): Source;
3
3
  //# 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,MAAM,EACX,KAAK,OAAO,EAGb,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/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"}
@@ -1,3 +1,3 @@
1
- import { type Source, type Request } from '../../../core/index.js';
1
+ import { type Request, type Source } from '../../../core/index.js';
2
2
  export declare function undici(request?: Partial<Request>): Source;
3
3
  //# 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,MAAM,EACX,KAAK,OAAO,EAGb,MAAM,eAAe,CAAA;AAEtB,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,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"}
@@ -1,8 +1,13 @@
1
- import type { TargetId, ClientId, Request } from './core/index.js';
1
+ import type { ClientId, Request, TargetId } from './core/index.js';
2
2
  import { undici } from './plugins/node/undici/index.js';
3
+ /**
4
+ * Generate code examples for HAR requests
5
+ */
3
6
  export declare function snippetz(): {
4
- get(target: TargetId, client: ClientId, request: Partial<Request>): import("./core/index.js").Source | undefined;
5
- print(target: TargetId, client: ClientId, request: Partial<Request>): string | undefined;
7
+ get(target: TargetId, client: ClientId, request: Partial<Request>): import("./core/index.js").Source | {
8
+ code: string;
9
+ };
10
+ print(target: TargetId, client: ClientId, request: Partial<Request>): string;
6
11
  targets(): TargetId[];
7
12
  clients(): ClientId[];
8
13
  plugins(): {
@@ -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,QAAQ,CAAA;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAM9C,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"}
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;;GAEG;AACH,wBAAgB,QAAQ;gBAIR,QAAQ,UAAU,QAAQ,WAAW,OAAO,CAAC,OAAO,CAAC;;;kBAWnD,QAAQ,UAAU,QAAQ,WAAW,OAAO,CAAC,OAAO,CAAC;;;;;;;uBAyBhD,QAAQ,UAAU,QAAQ;sBAO3B,MAAM,UAAU,MAAM;EAI3C"}
package/dist/snippetz.js CHANGED
@@ -4,6 +4,9 @@ import { fetch as fetch$1 } from './plugins/js/fetch/fetch.js';
4
4
  import { ofetch } from './plugins/js/ofetch/ofetch.js';
5
5
  import { ofetch as ofetch$1 } from './plugins/node/ofetch/ofetch.js';
6
6
 
7
+ /**
8
+ * Generate code examples for HAR requests
9
+ */
7
10
  function snippetz() {
8
11
  const plugins = [undici, fetch, fetch$1, ofetch, ofetch$1];
9
12
  return {
@@ -12,6 +15,9 @@ function snippetz() {
12
15
  if (plugin) {
13
16
  return plugin(request);
14
17
  }
18
+ return {
19
+ code: '',
20
+ };
15
21
  },
16
22
  print(target, client, request) {
17
23
  return this.get(target, client, request)?.code;
package/package.json CHANGED
@@ -2,19 +2,19 @@
2
2
  "name": "@scalar/snippetz",
3
3
  "license": "MIT",
4
4
  "author": "Scalar (https://github.com/scalar)",
5
- "homepage": "https://github.com/scalar/snippetz",
6
- "bugs": "https://github.com/scalar/snippetz/issues/new/choose",
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"
11
+ },
12
+ "version": "0.2.3",
7
13
  "engines": {
8
14
  "node": ">=18"
9
15
  },
10
- "version": "0.2.0",
11
16
  "type": "module",
12
- "files": [
13
- "dist",
14
- "CHANGELOG"
15
- ],
16
17
  "types": "./dist/index.d.ts",
17
- "module": "./dist/index.js",
18
18
  "exports": {
19
19
  ".": {
20
20
  "import": "./dist/index.js",
@@ -45,9 +45,14 @@
45
45
  "types": "./dist/core/index.d.ts"
46
46
  }
47
47
  },
48
+ "files": [
49
+ "dist",
50
+ "CHANGELOG"
51
+ ],
52
+ "module": "./dist/index.js",
48
53
  "devDependencies": {
49
- "@scalar/build-tooling": "^0.1.10",
50
- "@types/har-format": "^1.2.15"
54
+ "@types/har-format": "^1.2.15",
55
+ "@scalar/build-tooling": "0.1.10"
51
56
  },
52
57
  "scripts": {
53
58
  "build": "scalar-build-rollup",