@scalar/oas-utils 0.2.140 → 0.2.141
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/CHANGELOG.md +12 -0
- package/dist/entities/cookie/cookie.js +14 -13
- package/dist/entities/cookie/cookie.js.map +7 -0
- package/dist/entities/cookie/index.d.ts +1 -1
- package/dist/entities/cookie/index.d.ts.map +1 -1
- package/dist/entities/cookie/index.js +5 -1
- package/dist/entities/cookie/index.js.map +7 -0
- package/dist/entities/environment/environment.js +10 -9
- package/dist/entities/environment/environment.js.map +7 -0
- package/dist/entities/environment/index.d.ts +1 -1
- package/dist/entities/environment/index.d.ts.map +1 -1
- package/dist/entities/environment/index.js +5 -1
- package/dist/entities/environment/index.js.map +7 -0
- package/dist/entities/hotkeys/hotkeys.js +109 -116
- package/dist/entities/hotkeys/hotkeys.js.map +7 -0
- package/dist/entities/hotkeys/index.d.ts +1 -1
- package/dist/entities/hotkeys/index.d.ts.map +1 -1
- package/dist/entities/hotkeys/index.js +6 -1
- package/dist/entities/hotkeys/index.js.map +7 -0
- package/dist/entities/shared/index.d.ts +1 -1
- package/dist/entities/shared/index.d.ts.map +1 -1
- package/dist/entities/shared/index.js +7 -1
- package/dist/entities/shared/index.js.map +7 -0
- package/dist/entities/shared/utility.js +9 -12
- package/dist/entities/shared/utility.js.map +7 -0
- package/dist/entities/spec/collection.d.ts.map +1 -1
- package/dist/entities/spec/collection.js +91 -94
- package/dist/entities/spec/collection.js.map +7 -0
- package/dist/entities/spec/index.d.ts +8 -8
- package/dist/entities/spec/index.d.ts.map +1 -1
- package/dist/entities/spec/index.js +59 -9
- package/dist/entities/spec/index.js.map +7 -0
- package/dist/entities/spec/operation.d.ts +1 -1
- package/dist/entities/spec/operation.d.ts.map +1 -1
- package/dist/entities/spec/operation.js +5 -8
- package/dist/entities/spec/operation.js.map +7 -0
- package/dist/entities/spec/operation.test.js +43 -0
- package/dist/entities/spec/operation.test.js.map +7 -0
- package/dist/entities/spec/parameters.d.ts +1 -1
- package/dist/entities/spec/parameters.d.ts.map +1 -1
- package/dist/entities/spec/parameters.js +40 -38
- package/dist/entities/spec/parameters.js.map +7 -0
- package/dist/entities/spec/parameters.test.js +71 -0
- package/dist/entities/spec/parameters.test.js.map +7 -0
- package/dist/entities/spec/request-example.test.js +637 -0
- package/dist/entities/spec/request-example.test.js.map +7 -0
- package/dist/entities/spec/request-examples.d.ts +3 -3
- package/dist/entities/spec/request-examples.d.ts.map +1 -1
- package/dist/entities/spec/request-examples.js +323 -338
- package/dist/entities/spec/request-examples.js.map +7 -0
- package/dist/entities/spec/requests.d.ts +6 -8
- package/dist/entities/spec/requests.d.ts.map +1 -1
- package/dist/entities/spec/requests.js +80 -101
- package/dist/entities/spec/requests.js.map +7 -0
- package/dist/entities/spec/server.js +47 -63
- package/dist/entities/spec/server.js.map +7 -0
- package/dist/entities/spec/server.test.js +120 -0
- package/dist/entities/spec/server.test.js.map +7 -0
- package/dist/entities/spec/spec-objects.d.ts.map +1 -1
- package/dist/entities/spec/spec-objects.js +77 -121
- package/dist/entities/spec/spec-objects.js.map +7 -0
- package/dist/entities/spec/spec-objects.test.js +205 -0
- package/dist/entities/spec/spec-objects.test.js.map +7 -0
- package/dist/entities/spec/x-scalar-environments.js +21 -18
- package/dist/entities/spec/x-scalar-environments.js.map +7 -0
- package/dist/entities/spec/x-scalar-environments.test.js +11 -0
- package/dist/entities/spec/x-scalar-environments.test.js.map +7 -0
- package/dist/entities/spec/x-scalar-secrets.js +8 -6
- package/dist/entities/spec/x-scalar-secrets.js.map +7 -0
- package/dist/entities/spec/x-scalar-secrets.test.js +11 -0
- package/dist/entities/spec/x-scalar-secrets.test.js.map +7 -0
- package/dist/entities/workspace/index.d.ts +1 -1
- package/dist/entities/workspace/index.d.ts.map +1 -1
- package/dist/entities/workspace/index.js +7 -1
- package/dist/entities/workspace/index.js.map +7 -0
- package/dist/entities/workspace/workspace.js +46 -50
- package/dist/entities/workspace/workspace.js.map +7 -0
- package/dist/helpers/create-hash.js +15 -18
- package/dist/helpers/create-hash.js.map +7 -0
- package/dist/helpers/ensure-protocol.js +9 -10
- package/dist/helpers/ensure-protocol.js.map +7 -0
- package/dist/helpers/ensure-protocol.test.js +70 -0
- package/dist/helpers/ensure-protocol.test.js.map +7 -0
- package/dist/helpers/fetch-document.js +24 -32
- package/dist/helpers/fetch-document.js.map +7 -0
- package/dist/helpers/fetch-document.test.js +63 -0
- package/dist/helpers/fetch-document.test.js.map +7 -0
- package/dist/helpers/fetch-with-proxy-fallback.js +20 -28
- package/dist/helpers/fetch-with-proxy-fallback.js.map +7 -0
- package/dist/helpers/find-variables.d.ts +1 -1
- package/dist/helpers/find-variables.d.ts.map +1 -1
- package/dist/helpers/find-variables.js +6 -8
- package/dist/helpers/find-variables.js.map +7 -0
- package/dist/helpers/find-variables.test.js +20 -0
- package/dist/helpers/find-variables.test.js.map +7 -0
- package/dist/helpers/http-methods.d.ts +1 -1
- package/dist/helpers/http-methods.d.ts.map +1 -1
- package/dist/helpers/http-methods.js +60 -66
- package/dist/helpers/http-methods.js.map +7 -0
- package/dist/helpers/http-status-codes.js +319 -317
- package/dist/helpers/http-status-codes.js.map +7 -0
- package/dist/helpers/index.d.ts +27 -27
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/index.js +73 -27
- package/dist/helpers/index.js.map +7 -0
- package/dist/helpers/is-defined.js +5 -14
- package/dist/helpers/is-defined.js.map +7 -0
- package/dist/helpers/is-defined.test.js +37 -0
- package/dist/helpers/is-defined.test.js.map +7 -0
- package/dist/helpers/is-local-url.js +11 -16
- package/dist/helpers/is-local-url.js.map +7 -0
- package/dist/helpers/is-local-url.test.js +40 -0
- package/dist/helpers/is-local-url.test.js.map +7 -0
- package/dist/helpers/is-valid-url.js +9 -18
- package/dist/helpers/is-valid-url.js.map +7 -0
- package/dist/helpers/is-valid-url.test.js +17 -0
- package/dist/helpers/is-valid-url.test.js.map +7 -0
- package/dist/helpers/iterate-title.js +11 -15
- package/dist/helpers/iterate-title.js.map +7 -0
- package/dist/helpers/json2xml.js +40 -47
- package/dist/helpers/json2xml.js.map +7 -0
- package/dist/helpers/json2xml.test.js +19 -0
- package/dist/helpers/json2xml.test.js.map +7 -0
- package/dist/helpers/local-storage.js +13 -15
- package/dist/helpers/local-storage.js.map +7 -0
- package/dist/helpers/make-url-absolute.js +11 -17
- package/dist/helpers/make-url-absolute.js.map +7 -0
- package/dist/helpers/make-url-absolute.test.js +61 -0
- package/dist/helpers/make-url-absolute.test.js.map +7 -0
- package/dist/helpers/merge-urls.js +50 -75
- package/dist/helpers/merge-urls.js.map +7 -0
- package/dist/helpers/merge-urls.test.js +339 -0
- package/dist/helpers/merge-urls.test.js.map +7 -0
- package/dist/helpers/normalize-mime-type-object.js +21 -31
- package/dist/helpers/normalize-mime-type-object.js.map +7 -0
- package/dist/helpers/normalize-mime-type-object.test.js +53 -0
- package/dist/helpers/normalize-mime-type-object.test.js.map +7 -0
- package/dist/helpers/normalize-mime-type.js +8 -18
- package/dist/helpers/normalize-mime-type.js.map +7 -0
- package/dist/helpers/normalize-mime-type.test.js +33 -0
- package/dist/helpers/normalize-mime-type.test.js.map +7 -0
- package/dist/helpers/object.js +12 -18
- package/dist/helpers/object.js.map +7 -0
- package/dist/helpers/omit-undefined-values.js +17 -19
- package/dist/helpers/omit-undefined-values.js.map +7 -0
- package/dist/helpers/omit-undefined-values.test.js +89 -0
- package/dist/helpers/omit-undefined-values.test.js.map +7 -0
- package/dist/helpers/parse.js +71 -83
- package/dist/helpers/parse.js.map +7 -0
- package/dist/helpers/parse.test.js +45 -0
- package/dist/helpers/parse.test.js.map +7 -0
- package/dist/helpers/pretty-print-json.js +31 -35
- package/dist/helpers/pretty-print-json.js.map +7 -0
- package/dist/helpers/pretty-print-json.test.js +28 -0
- package/dist/helpers/pretty-print-json.test.js.map +7 -0
- package/dist/helpers/redirect-to-proxy.js +46 -83
- package/dist/helpers/redirect-to-proxy.js.map +7 -0
- package/dist/helpers/redirect-to-proxy.test.js +54 -0
- package/dist/helpers/redirect-to-proxy.test.js.map +7 -0
- package/dist/helpers/regex-helpers.js +11 -9
- package/dist/helpers/regex-helpers.js.map +7 -0
- package/dist/helpers/regex-helpers.test.js +154 -0
- package/dist/helpers/regex-helpers.test.js.map +7 -0
- package/dist/helpers/replace-variables.js +13 -16
- package/dist/helpers/replace-variables.js.map +7 -0
- package/dist/helpers/replace-variables.test.js +30 -0
- package/dist/helpers/replace-variables.test.js.map +7 -0
- package/dist/helpers/schema-model.js +15 -14
- package/dist/helpers/schema-model.js.map +7 -0
- package/dist/helpers/security/get-schemes.d.ts +1 -1
- package/dist/helpers/security/get-schemes.d.ts.map +1 -1
- package/dist/helpers/security/get-schemes.js +7 -9
- package/dist/helpers/security/get-schemes.js.map +7 -0
- package/dist/helpers/security/get-schemes.test.js +71 -0
- package/dist/helpers/security/get-schemes.test.js.map +7 -0
- package/dist/helpers/security/has-token.js +18 -19
- package/dist/helpers/security/has-token.js.map +7 -0
- package/dist/helpers/security/has-token.test.js +157 -0
- package/dist/helpers/security/has-token.test.js.map +7 -0
- package/dist/helpers/security/index.d.ts +2 -2
- package/dist/helpers/security/index.d.ts.map +1 -1
- package/dist/helpers/security/index.js +7 -2
- package/dist/helpers/security/index.js.map +7 -0
- package/dist/helpers/should-ignore-entity.d.ts +2 -2
- package/dist/helpers/should-ignore-entity.d.ts.map +1 -1
- package/dist/helpers/should-ignore-entity.js +5 -6
- package/dist/helpers/should-ignore-entity.js.map +7 -0
- package/dist/helpers/ssr-state.js +6 -6
- package/dist/helpers/ssr-state.js.map +7 -0
- package/dist/helpers/string.js +6 -18
- package/dist/helpers/string.js.map +7 -0
- package/dist/migrations/data-version.js +7 -17
- package/dist/migrations/data-version.js.map +7 -0
- package/dist/migrations/generate-types.js +56 -0
- package/dist/migrations/generate-types.js.map +7 -0
- package/dist/migrations/index.d.ts +3 -3
- package/dist/migrations/index.d.ts.map +1 -1
- package/dist/migrations/index.js +10 -3
- package/dist/migrations/index.js.map +7 -0
- package/dist/migrations/local-storage.d.ts +1 -1
- package/dist/migrations/local-storage.d.ts.map +1 -1
- package/dist/migrations/local-storage.js +27 -32
- package/dist/migrations/local-storage.js.map +7 -0
- package/dist/migrations/migrator.d.ts +1 -1
- package/dist/migrations/migrator.d.ts.map +1 -1
- package/dist/migrations/migrator.js +51 -58
- package/dist/migrations/migrator.js.map +7 -0
- package/dist/migrations/semver.js +23 -25
- package/dist/migrations/semver.js.map +7 -0
- package/dist/migrations/semver.test.js +21 -0
- package/dist/migrations/semver.test.js.map +7 -0
- package/dist/migrations/v-0.0.0/types.generated.js +1 -0
- package/dist/migrations/v-0.0.0/types.generated.js.map +7 -0
- package/dist/migrations/v-2.1.0/migration.d.ts +2 -2
- package/dist/migrations/v-2.1.0/migration.d.ts.map +1 -1
- package/dist/migrations/v-2.1.0/migration.js +256 -273
- package/dist/migrations/v-2.1.0/migration.js.map +7 -0
- package/dist/migrations/v-2.1.0/types.generated.js +1 -0
- package/dist/migrations/v-2.1.0/types.generated.js.map +7 -0
- package/dist/migrations/v-2.2.0/migration.d.ts +2 -2
- package/dist/migrations/v-2.2.0/migration.d.ts.map +1 -1
- package/dist/migrations/v-2.2.0/migration.js +95 -100
- package/dist/migrations/v-2.2.0/migration.js.map +7 -0
- package/dist/migrations/v-2.2.0/types.generated.js +1 -0
- package/dist/migrations/v-2.2.0/types.generated.js.map +7 -0
- package/dist/migrations/v-2.3.0/migration.d.ts +2 -2
- package/dist/migrations/v-2.3.0/migration.d.ts.map +1 -1
- package/dist/migrations/v-2.3.0/migration.js +47 -46
- package/dist/migrations/v-2.3.0/migration.js.map +7 -0
- package/dist/migrations/v-2.3.0/types.generated.js +1 -0
- package/dist/migrations/v-2.3.0/types.generated.js.map +7 -0
- package/dist/migrations/v-2.4.0/migration.d.ts +2 -2
- package/dist/migrations/v-2.4.0/migration.d.ts.map +1 -1
- package/dist/migrations/v-2.4.0/migration.js +24 -26
- package/dist/migrations/v-2.4.0/migration.js.map +7 -0
- package/dist/migrations/v-2.4.0/migration.test.js +90 -0
- package/dist/migrations/v-2.4.0/migration.test.js.map +7 -0
- package/dist/migrations/v-2.4.0/types.generated.js +1 -0
- package/dist/migrations/v-2.4.0/types.generated.js.map +7 -0
- package/dist/migrations/v-2.5.0/migration.d.ts +2 -2
- package/dist/migrations/v-2.5.0/migration.d.ts.map +1 -1
- package/dist/migrations/v-2.5.0/migration.js +135 -119
- package/dist/migrations/v-2.5.0/migration.js.map +7 -0
- package/dist/migrations/v-2.5.0/migration.test.js +108 -0
- package/dist/migrations/v-2.5.0/migration.test.js.map +7 -0
- package/dist/migrations/v-2.5.0/types.generated.d.ts +8 -8
- package/dist/migrations/v-2.5.0/types.generated.d.ts.map +1 -1
- package/dist/migrations/v-2.5.0/types.generated.js +1 -0
- package/dist/migrations/v-2.5.0/types.generated.js.map +7 -0
- package/dist/spec-getters/get-example-from-schema.js +206 -268
- package/dist/spec-getters/get-example-from-schema.js.map +7 -0
- package/dist/spec-getters/get-example-from-schema.test.js +1092 -0
- package/dist/spec-getters/get-example-from-schema.test.js.map +7 -0
- package/dist/spec-getters/get-parameters-from-operation.js +22 -42
- package/dist/spec-getters/get-parameters-from-operation.js.map +7 -0
- package/dist/spec-getters/get-parameters-from-operation.test.js +178 -0
- package/dist/spec-getters/get-parameters-from-operation.test.js.map +7 -0
- package/dist/spec-getters/get-request-body-from-operation.js +110 -162
- package/dist/spec-getters/get-request-body-from-operation.js.map +7 -0
- package/dist/spec-getters/get-request-body-from-operation.test.js +289 -0
- package/dist/spec-getters/get-request-body-from-operation.test.js.map +7 -0
- package/dist/spec-getters/get-server-variable-examples.d.ts +1 -1
- package/dist/spec-getters/get-server-variable-examples.d.ts.map +1 -1
- package/dist/spec-getters/get-server-variable-examples.js +12 -11
- package/dist/spec-getters/get-server-variable-examples.js.map +7 -0
- package/dist/spec-getters/index.d.ts +4 -4
- package/dist/spec-getters/index.d.ts.map +1 -1
- package/dist/spec-getters/index.js +11 -4
- package/dist/spec-getters/index.js.map +7 -0
- package/dist/transforms/import-spec.d.ts +6 -6
- package/dist/transforms/import-spec.d.ts.map +1 -1
- package/dist/transforms/import-spec.js +313 -425
- package/dist/transforms/import-spec.js.map +7 -0
- package/dist/transforms/import-spec.test.js +1124 -0
- package/dist/transforms/import-spec.test.js.map +7 -0
- package/dist/transforms/index.d.ts +1 -1
- package/dist/transforms/index.d.ts.map +1 -1
- package/dist/transforms/index.js +7 -1
- package/dist/transforms/index.js.map +7 -0
- package/package.json +9 -10
package/dist/helpers/object.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
// TODO: This is a copy of packages/core-interface/src/database/utility.ts
|
|
2
|
-
/**
|
|
3
|
-
* Overwrite a target object a new replacement object handling removed keys
|
|
4
|
-
*/
|
|
5
1
|
function objectMerge(target, replacement) {
|
|
6
|
-
|
|
7
|
-
Object.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return target;
|
|
2
|
+
Object.keys(target).forEach((key) => {
|
|
3
|
+
if (!Object.hasOwn(replacement, key)) {
|
|
4
|
+
delete target[key];
|
|
5
|
+
}
|
|
6
|
+
});
|
|
7
|
+
Object.assign(target, replacement);
|
|
8
|
+
return target;
|
|
14
9
|
}
|
|
15
|
-
/**
|
|
16
|
-
* Type safe version of Object.keys
|
|
17
|
-
* Can probably remove this whenever typescript adds it
|
|
18
|
-
*/
|
|
19
10
|
const getObjectKeys = (obj) => Object.keys(obj);
|
|
20
|
-
|
|
21
|
-
|
|
11
|
+
export {
|
|
12
|
+
getObjectKeys,
|
|
13
|
+
objectMerge
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=object.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/helpers/object.ts"],
|
|
4
|
+
"sourcesContent": ["// TODO: This is a copy of packages/core-interface/src/database/utility.ts\n/**\n * Overwrite a target object a new replacement object handling removed keys\n */\nexport function objectMerge<A extends object, B extends object>(target: A, replacement: B) {\n // Clear any keys that have been removed in the replacement\n Object.keys(target).forEach((key) => {\n if (!Object.hasOwn(replacement, key)) {\n delete target[key as keyof A]\n }\n })\n\n Object.assign(target, replacement)\n\n return target as unknown as B\n}\n\n/**\n * Type safe version of Object.keys\n * Can probably remove this whenever typescript adds it\n */\nexport const getObjectKeys = <T extends object>(obj: T): (keyof T)[] => Object.keys(obj) as (keyof T)[]\n"],
|
|
5
|
+
"mappings": "AAIO,SAAS,YAAgD,QAAW,aAAgB;AAEzF,SAAO,KAAK,MAAM,EAAE,QAAQ,CAAC,QAAQ;AACnC,QAAI,CAAC,OAAO,OAAO,aAAa,GAAG,GAAG;AACpC,aAAO,OAAO,GAAc;AAAA,IAC9B;AAAA,EACF,CAAC;AAED,SAAO,OAAO,QAAQ,WAAW;AAEjC,SAAO;AACT;AAMO,MAAM,gBAAgB,CAAmB,QAAwB,OAAO,KAAK,GAAG;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,21 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Removes undefined values from an object.
|
|
3
|
-
*
|
|
4
|
-
* Can be used as a transform function for any Zod schema.
|
|
5
|
-
*/
|
|
6
1
|
const omitUndefinedValues = (data) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
})
|
|
2
|
+
if (Array.isArray(data)) {
|
|
3
|
+
return data.map(
|
|
4
|
+
(item) => typeof item === "object" && item !== null ? omitUndefinedValues(item) : item
|
|
5
|
+
);
|
|
6
|
+
}
|
|
7
|
+
return Object.fromEntries(
|
|
8
|
+
Object.entries(data).filter(([_, value]) => value !== void 0).map(([key, value]) => {
|
|
9
|
+
if (typeof value === "object" && value !== null) {
|
|
10
|
+
return [key, omitUndefinedValues(value)];
|
|
11
|
+
}
|
|
12
|
+
return [key, value];
|
|
13
|
+
})
|
|
14
|
+
);
|
|
19
15
|
};
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
export {
|
|
17
|
+
omitUndefinedValues
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=omit-undefined-values.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/helpers/omit-undefined-values.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Removes undefined values from an object.\n *\n * Can be used as a transform function for any Zod schema.\n */\nexport const omitUndefinedValues = <T extends object>(data: T): T => {\n // Handle arrays specially\n if (Array.isArray(data)) {\n return data.map((item) =>\n typeof item === 'object' && item !== null ? omitUndefinedValues(item) : item,\n ) as unknown as T\n }\n\n return Object.fromEntries(\n Object.entries(data)\n .filter(([_, value]) => value !== undefined)\n .map(([key, value]) => {\n if (typeof value === 'object' && value !== null) {\n return [key, omitUndefinedValues(value)]\n }\n return [key, value]\n }),\n ) as T\n}\n"],
|
|
5
|
+
"mappings": "AAKO,MAAM,sBAAsB,CAAmB,SAAe;AAEnE,MAAI,MAAM,QAAQ,IAAI,GAAG;AACvB,WAAO,KAAK;AAAA,MAAI,CAAC,SACf,OAAO,SAAS,YAAY,SAAS,OAAO,oBAAoB,IAAI,IAAI;AAAA,IAC1E;AAAA,EACF;AAEA,SAAO,OAAO;AAAA,IACZ,OAAO,QAAQ,IAAI,EAChB,OAAO,CAAC,CAAC,GAAG,KAAK,MAAM,UAAU,MAAS,EAC1C,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AACrB,UAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,eAAO,CAAC,KAAK,oBAAoB,KAAK,CAAC;AAAA,MACzC;AACA,aAAO,CAAC,KAAK,KAAK;AAAA,IACpB,CAAC;AAAA,EACL;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { omitUndefinedValues } from "./omit-undefined-values.js";
|
|
3
|
+
describe("omitUndefinedValues", () => {
|
|
4
|
+
it("removes undefined values from flat objects", () => {
|
|
5
|
+
const input = {
|
|
6
|
+
name: "test",
|
|
7
|
+
description: void 0,
|
|
8
|
+
value: 123,
|
|
9
|
+
empty: void 0
|
|
10
|
+
};
|
|
11
|
+
const result = omitUndefinedValues(input);
|
|
12
|
+
expect(result).toEqual({
|
|
13
|
+
name: "test",
|
|
14
|
+
value: 123
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
it("preserves null values", () => {
|
|
18
|
+
const input = {
|
|
19
|
+
name: "test",
|
|
20
|
+
description: null,
|
|
21
|
+
value: void 0
|
|
22
|
+
};
|
|
23
|
+
const result = omitUndefinedValues(input);
|
|
24
|
+
expect(result).toEqual({
|
|
25
|
+
name: "test",
|
|
26
|
+
description: null
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
it("handles nested objects", () => {
|
|
30
|
+
const input = {
|
|
31
|
+
name: "test",
|
|
32
|
+
metadata: {
|
|
33
|
+
description: void 0,
|
|
34
|
+
tags: ["a", "b"],
|
|
35
|
+
settings: {
|
|
36
|
+
enabled: true,
|
|
37
|
+
config: void 0
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const result = omitUndefinedValues(input);
|
|
42
|
+
expect(result).toEqual({
|
|
43
|
+
name: "test",
|
|
44
|
+
metadata: {
|
|
45
|
+
tags: ["a", "b"],
|
|
46
|
+
settings: {
|
|
47
|
+
enabled: true
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
it("handles empty objects", () => {
|
|
53
|
+
const input = {};
|
|
54
|
+
const result = omitUndefinedValues(input);
|
|
55
|
+
expect(result).toEqual({});
|
|
56
|
+
});
|
|
57
|
+
it("preserves arrays", () => {
|
|
58
|
+
const input = {
|
|
59
|
+
items: [
|
|
60
|
+
{ id: 1, name: "first", description: void 0 },
|
|
61
|
+
{ id: 2, name: "second", value: void 0 }
|
|
62
|
+
]
|
|
63
|
+
};
|
|
64
|
+
const result = omitUndefinedValues(input);
|
|
65
|
+
expect(result).toEqual({
|
|
66
|
+
items: [
|
|
67
|
+
{ id: 1, name: "first" },
|
|
68
|
+
{ id: 2, name: "second" }
|
|
69
|
+
]
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
it("preserves falsy values except undefined", () => {
|
|
73
|
+
const input = {
|
|
74
|
+
zero: 0,
|
|
75
|
+
empty: "",
|
|
76
|
+
falsy: false,
|
|
77
|
+
nullish: null,
|
|
78
|
+
notDefined: void 0
|
|
79
|
+
};
|
|
80
|
+
const result = omitUndefinedValues(input);
|
|
81
|
+
expect(result).toEqual({
|
|
82
|
+
zero: 0,
|
|
83
|
+
empty: "",
|
|
84
|
+
falsy: false,
|
|
85
|
+
nullish: null
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
//# sourceMappingURL=omit-undefined-values.test.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/helpers/omit-undefined-values.test.ts"],
|
|
4
|
+
"sourcesContent": ["import { describe, expect, it } from 'vitest'\nimport { omitUndefinedValues } from './omit-undefined-values'\n\ndescribe('omitUndefinedValues', () => {\n it('removes undefined values from flat objects', () => {\n const input = {\n name: 'test',\n description: undefined,\n value: 123,\n empty: undefined,\n }\n\n const result = omitUndefinedValues(input)\n\n expect(result).toEqual({\n name: 'test',\n value: 123,\n })\n })\n\n it('preserves null values', () => {\n const input = {\n name: 'test',\n description: null,\n value: undefined,\n }\n\n const result = omitUndefinedValues(input)\n\n expect(result).toEqual({\n name: 'test',\n description: null,\n })\n })\n\n it('handles nested objects', () => {\n const input = {\n name: 'test',\n metadata: {\n description: undefined,\n tags: ['a', 'b'],\n settings: {\n enabled: true,\n config: undefined,\n },\n },\n }\n\n const result = omitUndefinedValues(input)\n\n expect(result).toEqual({\n name: 'test',\n metadata: {\n tags: ['a', 'b'],\n settings: {\n enabled: true,\n },\n },\n })\n })\n\n it('handles empty objects', () => {\n const input = {}\n\n const result = omitUndefinedValues(input)\n\n expect(result).toEqual({})\n })\n\n it('preserves arrays', () => {\n const input = {\n items: [\n { id: 1, name: 'first', description: undefined },\n { id: 2, name: 'second', value: undefined },\n ],\n }\n\n const result = omitUndefinedValues(input)\n\n expect(result).toEqual({\n items: [\n { id: 1, name: 'first' },\n { id: 2, name: 'second' },\n ],\n })\n })\n\n it('preserves falsy values except undefined', () => {\n const input = {\n zero: 0,\n empty: '',\n falsy: false,\n nullish: null,\n notDefined: undefined,\n }\n\n const result = omitUndefinedValues(input)\n\n expect(result).toEqual({\n zero: 0,\n empty: '',\n falsy: false,\n nullish: null,\n })\n })\n})\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,UAAU,QAAQ,UAAU;AACrC,SAAS,2BAA2B;AAEpC,SAAS,uBAAuB,MAAM;AACpC,KAAG,8CAA8C,MAAM;AACrD,UAAM,QAAQ;AAAA,MACZ,MAAM;AAAA,MACN,aAAa;AAAA,MACb,OAAO;AAAA,MACP,OAAO;AAAA,IACT;AAEA,UAAM,SAAS,oBAAoB,KAAK;AAExC,WAAO,MAAM,EAAE,QAAQ;AAAA,MACrB,MAAM;AAAA,MACN,OAAO;AAAA,IACT,CAAC;AAAA,EACH,CAAC;AAED,KAAG,yBAAyB,MAAM;AAChC,UAAM,QAAQ;AAAA,MACZ,MAAM;AAAA,MACN,aAAa;AAAA,MACb,OAAO;AAAA,IACT;AAEA,UAAM,SAAS,oBAAoB,KAAK;AAExC,WAAO,MAAM,EAAE,QAAQ;AAAA,MACrB,MAAM;AAAA,MACN,aAAa;AAAA,IACf,CAAC;AAAA,EACH,CAAC;AAED,KAAG,0BAA0B,MAAM;AACjC,UAAM,QAAQ;AAAA,MACZ,MAAM;AAAA,MACN,UAAU;AAAA,QACR,aAAa;AAAA,QACb,MAAM,CAAC,KAAK,GAAG;AAAA,QACf,UAAU;AAAA,UACR,SAAS;AAAA,UACT,QAAQ;AAAA,QACV;AAAA,MACF;AAAA,IACF;AAEA,UAAM,SAAS,oBAAoB,KAAK;AAExC,WAAO,MAAM,EAAE,QAAQ;AAAA,MACrB,MAAM;AAAA,MACN,UAAU;AAAA,QACR,MAAM,CAAC,KAAK,GAAG;AAAA,QACf,UAAU;AAAA,UACR,SAAS;AAAA,QACX;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAED,KAAG,yBAAyB,MAAM;AAChC,UAAM,QAAQ,CAAC;AAEf,UAAM,SAAS,oBAAoB,KAAK;AAExC,WAAO,MAAM,EAAE,QAAQ,CAAC,CAAC;AAAA,EAC3B,CAAC;AAED,KAAG,oBAAoB,MAAM;AAC3B,UAAM,QAAQ;AAAA,MACZ,OAAO;AAAA,QACL,EAAE,IAAI,GAAG,MAAM,SAAS,aAAa,OAAU;AAAA,QAC/C,EAAE,IAAI,GAAG,MAAM,UAAU,OAAO,OAAU;AAAA,MAC5C;AAAA,IACF;AAEA,UAAM,SAAS,oBAAoB,KAAK;AAExC,WAAO,MAAM,EAAE,QAAQ;AAAA,MACrB,OAAO;AAAA,QACL,EAAE,IAAI,GAAG,MAAM,QAAQ;AAAA,QACvB,EAAE,IAAI,GAAG,MAAM,SAAS;AAAA,MAC1B;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAED,KAAG,2CAA2C,MAAM;AAClD,UAAM,QAAQ;AAAA,MACZ,MAAM;AAAA,MACN,OAAO;AAAA,MACP,OAAO;AAAA,MACP,SAAS;AAAA,MACT,YAAY;AAAA,IACd;AAEA,UAAM,SAAS,oBAAoB,KAAK;AAExC,WAAO,MAAM,EAAE,QAAQ;AAAA,MACrB,MAAM;AAAA,MACN,OAAO;AAAA,MACP,OAAO;AAAA,MACP,SAAS;AAAA,IACX,CAAC;AAAA,EACH,CAAC;AACH,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/helpers/parse.js
CHANGED
|
@@ -1,95 +1,83 @@
|
|
|
1
|
-
import { parse, stringify } from
|
|
2
|
-
|
|
3
|
-
/** Yaml handling with optional safeparse */
|
|
1
|
+
import { parse, stringify } from "yaml";
|
|
4
2
|
const yaml = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
stringify,
|
|
3
|
+
/** Parse and throw if the return value is not an object */
|
|
4
|
+
parse: (val) => {
|
|
5
|
+
const yamlObject = parse(val);
|
|
6
|
+
if (typeof yamlObject !== "object") {
|
|
7
|
+
throw Error("Invalid YAML object");
|
|
8
|
+
}
|
|
9
|
+
return yamlObject;
|
|
10
|
+
},
|
|
11
|
+
/** Parse and return a fallback on failure */
|
|
12
|
+
parseSafe(val, fallback) {
|
|
13
|
+
try {
|
|
14
|
+
return yaml.parse(val);
|
|
15
|
+
} catch (err) {
|
|
16
|
+
return typeof fallback === "function" ? fallback(err) : fallback;
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
stringify
|
|
23
20
|
};
|
|
24
|
-
/** JSON handling with optional safeparse */
|
|
25
21
|
const json = {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
stringify: (val) => JSON.stringify(val),
|
|
22
|
+
/** Parse and throw if the return value is not an object */
|
|
23
|
+
parse: (val) => {
|
|
24
|
+
const jsonObject = JSON.parse(val);
|
|
25
|
+
if (typeof jsonObject !== "object") {
|
|
26
|
+
throw Error("Invalid JSON object");
|
|
27
|
+
}
|
|
28
|
+
return jsonObject;
|
|
29
|
+
},
|
|
30
|
+
/** Parse and return a fallback on failure */
|
|
31
|
+
parseSafe(val, fallback) {
|
|
32
|
+
try {
|
|
33
|
+
return json.parse(val);
|
|
34
|
+
} catch (err) {
|
|
35
|
+
return typeof fallback === "function" ? fallback(err) : fallback;
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
stringify: (val) => JSON.stringify(val)
|
|
44
39
|
};
|
|
45
|
-
/**
|
|
46
|
-
* Check if value is a valid JSON string
|
|
47
|
-
*/
|
|
48
40
|
const isJsonString = (value) => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
41
|
+
if (typeof value !== "string") {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return !!json.parseSafe(value, false);
|
|
53
45
|
};
|
|
54
|
-
/**
|
|
55
|
-
* This helper is used to transform the content of the swagger file to JSON, even it was YAML.
|
|
56
|
-
*/
|
|
57
46
|
const transformToJson = (value) => {
|
|
58
|
-
|
|
59
|
-
return JSON.stringify(json.parseSafe(value, yaml.parseSafe(value, value)));
|
|
47
|
+
return JSON.stringify(json.parseSafe(value, yaml.parseSafe(value, value)));
|
|
60
48
|
};
|
|
61
|
-
/** Validates a JSON string if provided. Otherwise returns the raw YAML */
|
|
62
49
|
function formatJsonOrYamlString(value) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
catch {
|
|
73
|
-
// YAML
|
|
74
|
-
return value;
|
|
75
|
-
}
|
|
50
|
+
const trimmed = value.trim();
|
|
51
|
+
if (trimmed[0] !== "{" && trimmed[0] !== "[") {
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
return JSON.stringify(JSON.parse(value), null, 2);
|
|
56
|
+
} catch {
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
76
59
|
}
|
|
77
|
-
/** Parse JSON or YAML into an object */
|
|
78
60
|
const parseJsonOrYaml = (value) => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
61
|
+
if (typeof value !== "string") {
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
const jsonObject = json.parseSafe(value, null);
|
|
65
|
+
if (jsonObject) {
|
|
66
|
+
return jsonObject;
|
|
67
|
+
}
|
|
68
|
+
if (value.length > 0 && ["{", "["].includes(value[0] ?? "")) {
|
|
69
|
+
throw Error("Invalid JSON or YAML");
|
|
70
|
+
}
|
|
71
|
+
return yaml.parseSafe(value, (err) => {
|
|
72
|
+
throw Error(err);
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
export {
|
|
76
|
+
formatJsonOrYamlString,
|
|
77
|
+
isJsonString,
|
|
78
|
+
json,
|
|
79
|
+
parseJsonOrYaml,
|
|
80
|
+
transformToJson,
|
|
81
|
+
yaml
|
|
93
82
|
};
|
|
94
|
-
|
|
95
|
-
export { formatJsonOrYamlString, isJsonString, json, parseJsonOrYaml, transformToJson, yaml };
|
|
83
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/helpers/parse.ts"],
|
|
4
|
+
"sourcesContent": ["import type { UnknownObject } from '@scalar/types/utils'\nimport { parse, stringify } from 'yaml'\n\ntype PrimitiveOrObject = object | string | null | number | boolean | undefined\n\n/** Yaml handling with optional safeparse */\nexport const yaml = {\n /** Parse and throw if the return value is not an object */\n parse: (val: string) => {\n const yamlObject = parse(val)\n if (typeof yamlObject !== 'object') {\n throw Error('Invalid YAML object')\n }\n return yamlObject as UnknownObject\n },\n /** Parse and return a fallback on failure */\n parseSafe<T extends PrimitiveOrObject>(val: string, fallback: T | ((err: any) => T)): UnknownObject | T {\n try {\n return yaml.parse(val)\n } catch (err: any) {\n return typeof fallback === 'function' ? fallback(err) : fallback\n }\n },\n stringify,\n}\n\n/** JSON handling with optional safeparse */\nexport const json = {\n /** Parse and throw if the return value is not an object */\n parse: (val: string): UnknownObject => {\n const jsonObject = JSON.parse(val)\n if (typeof jsonObject !== 'object') {\n throw Error('Invalid JSON object')\n }\n return jsonObject\n },\n /** Parse and return a fallback on failure */\n parseSafe<T extends PrimitiveOrObject>(val: string, fallback: T | ((err: any) => T)): UnknownObject | T {\n try {\n return json.parse(val)\n } catch (err) {\n return typeof fallback === 'function' ? fallback(err) : fallback\n }\n },\n stringify: (val: object) => JSON.stringify(val),\n}\n\n/**\n * Check if value is a valid JSON string\n */\nexport const isJsonString = (value?: any) => {\n if (typeof value !== 'string') {\n return false\n }\n\n return !!json.parseSafe(value, false)\n}\n\n/**\n * This helper is used to transform the content of the swagger file to JSON, even it was YAML.\n */\nexport const transformToJson = (value: string) => {\n // Try json, then fallback to yaml, then fallback to string\n return JSON.stringify(json.parseSafe(value, yaml.parseSafe(value, value)))\n}\n\n/** Validates a JSON string if provided. Otherwise returns the raw YAML */\nexport function formatJsonOrYamlString(value: string) {\n // If we don't start with a bracket assume yaml\n const trimmed = value.trim()\n if (trimmed[0] !== '{' && trimmed[0] !== '[') {\n return value\n }\n\n try {\n // JSON\n return JSON.stringify(JSON.parse(value), null, 2)\n } catch {\n // YAML\n return value\n }\n}\n\n/** Parse JSON or YAML into an object */\nexport const parseJsonOrYaml = (value: string | UnknownObject): UnknownObject => {\n if (typeof value !== 'string') {\n return value\n }\n\n const jsonObject = json.parseSafe(value, null)\n if (jsonObject) {\n return jsonObject\n }\n\n // Value is probably supposed to be JSON. Throw\n if (value.length > 0 && ['{', '['].includes(value[0] ?? '')) {\n throw Error('Invalid JSON or YAML')\n }\n\n return yaml.parseSafe(value, (err) => {\n throw Error(err)\n })\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,OAAO,iBAAiB;AAK1B,MAAM,OAAO;AAAA;AAAA,EAElB,OAAO,CAAC,QAAgB;AACtB,UAAM,aAAa,MAAM,GAAG;AAC5B,QAAI,OAAO,eAAe,UAAU;AAClC,YAAM,MAAM,qBAAqB;AAAA,IACnC;AACA,WAAO;AAAA,EACT;AAAA;AAAA,EAEA,UAAuC,KAAa,UAAoD;AACtG,QAAI;AACF,aAAO,KAAK,MAAM,GAAG;AAAA,IACvB,SAAS,KAAU;AACjB,aAAO,OAAO,aAAa,aAAa,SAAS,GAAG,IAAI;AAAA,IAC1D;AAAA,EACF;AAAA,EACA;AACF;AAGO,MAAM,OAAO;AAAA;AAAA,EAElB,OAAO,CAAC,QAA+B;AACrC,UAAM,aAAa,KAAK,MAAM,GAAG;AACjC,QAAI,OAAO,eAAe,UAAU;AAClC,YAAM,MAAM,qBAAqB;AAAA,IACnC;AACA,WAAO;AAAA,EACT;AAAA;AAAA,EAEA,UAAuC,KAAa,UAAoD;AACtG,QAAI;AACF,aAAO,KAAK,MAAM,GAAG;AAAA,IACvB,SAAS,KAAK;AACZ,aAAO,OAAO,aAAa,aAAa,SAAS,GAAG,IAAI;AAAA,IAC1D;AAAA,EACF;AAAA,EACA,WAAW,CAAC,QAAgB,KAAK,UAAU,GAAG;AAChD;AAKO,MAAM,eAAe,CAAC,UAAgB;AAC3C,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO;AAAA,EACT;AAEA,SAAO,CAAC,CAAC,KAAK,UAAU,OAAO,KAAK;AACtC;AAKO,MAAM,kBAAkB,CAAC,UAAkB;AAEhD,SAAO,KAAK,UAAU,KAAK,UAAU,OAAO,KAAK,UAAU,OAAO,KAAK,CAAC,CAAC;AAC3E;AAGO,SAAS,uBAAuB,OAAe;AAEpD,QAAM,UAAU,MAAM,KAAK;AAC3B,MAAI,QAAQ,CAAC,MAAM,OAAO,QAAQ,CAAC,MAAM,KAAK;AAC5C,WAAO;AAAA,EACT;AAEA,MAAI;AAEF,WAAO,KAAK,UAAU,KAAK,MAAM,KAAK,GAAG,MAAM,CAAC;AAAA,EAClD,QAAQ;AAEN,WAAO;AAAA,EACT;AACF;AAGO,MAAM,kBAAkB,CAAC,UAAiD;AAC/E,MAAI,OAAO,UAAU,UAAU;AAC7B,WAAO;AAAA,EACT;AAEA,QAAM,aAAa,KAAK,UAAU,OAAO,IAAI;AAC7C,MAAI,YAAY;AACd,WAAO;AAAA,EACT;AAGA,MAAI,MAAM,SAAS,KAAK,CAAC,KAAK,GAAG,EAAE,SAAS,MAAM,CAAC,KAAK,EAAE,GAAG;AAC3D,UAAM,MAAM,sBAAsB;AAAA,EACpC;AAEA,SAAO,KAAK,UAAU,OAAO,CAAC,QAAQ;AACpC,UAAM,MAAM,GAAG;AAAA,EACjB,CAAC;AACH;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { isJsonString, json, parseJsonOrYaml, transformToJson, yaml } from "./parse.js";
|
|
3
|
+
describe("Handles yaml and json parsing", () => {
|
|
4
|
+
it("Parses basic yaml", () => {
|
|
5
|
+
expect(yaml.parse("openapi: 3.0.0")).toEqual({
|
|
6
|
+
openapi: "3.0.0"
|
|
7
|
+
});
|
|
8
|
+
expect(json.parse('{ "openapi": "3.0.0" }')).toEqual({
|
|
9
|
+
openapi: "3.0.0"
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
it("Fails if any type except an object is not returned", () => {
|
|
13
|
+
expect(() => yaml.parse("10")).toThrowError();
|
|
14
|
+
expect(() => json.parse("10")).toThrowError();
|
|
15
|
+
});
|
|
16
|
+
it("transforms Yaml to JSON", () => {
|
|
17
|
+
expect(transformToJson("openapi: 3.0.0")).toMatchObject(JSON.stringify({ openapi: "3.0.0" }));
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
describe("isJsonString", () => {
|
|
21
|
+
it("keeps a path as is", async () => {
|
|
22
|
+
expect(isJsonString("foobar")).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
it("removes slash", async () => {
|
|
25
|
+
expect(isJsonString('{ "foo": "bar" }')).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
it("trims whitespace", async () => {
|
|
28
|
+
expect(isJsonString({ foo: "bar" })).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
describe("parseJsonOrYaml", () => {
|
|
32
|
+
it("Handles json", () => {
|
|
33
|
+
expect(parseJsonOrYaml('{ "a": 10 }')).toEqual({ a: 10 });
|
|
34
|
+
});
|
|
35
|
+
it("Handles YAML", () => {
|
|
36
|
+
expect(parseJsonOrYaml("a: 10")).toEqual({ a: 10 });
|
|
37
|
+
});
|
|
38
|
+
it("Throws for invalid json or yaml", () => {
|
|
39
|
+
expect(() => parseJsonOrYaml("asdasdad: 0--- \n--")).toThrowError();
|
|
40
|
+
});
|
|
41
|
+
it("Throws if an object is not returned", () => {
|
|
42
|
+
expect(() => parseJsonOrYaml("asda")).toThrowError();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=parse.test.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/helpers/parse.test.ts"],
|
|
4
|
+
"sourcesContent": ["import { describe, expect, it } from 'vitest'\n\nimport { isJsonString, json, parseJsonOrYaml, transformToJson, yaml } from './parse'\n\ndescribe('Handles yaml and json parsing', () => {\n it('Parses basic yaml', () => {\n expect(yaml.parse('openapi: 3.0.0')).toEqual({\n openapi: '3.0.0',\n })\n\n expect(json.parse('{ \"openapi\": \"3.0.0\" }')).toEqual({\n openapi: '3.0.0',\n })\n })\n\n it('Fails if any type except an object is not returned', () => {\n expect(() => yaml.parse('10')).toThrowError()\n expect(() => json.parse('10')).toThrowError()\n })\n\n it('transforms Yaml to JSON', () => {\n expect(transformToJson('openapi: 3.0.0')).toMatchObject(JSON.stringify({ openapi: '3.0.0' }))\n })\n})\n\ndescribe('isJsonString', () => {\n it('keeps a path as is', async () => {\n expect(isJsonString('foobar')).toBe(false)\n })\n\n it('removes slash', async () => {\n expect(isJsonString('{ \"foo\": \"bar\" }')).toBe(true)\n })\n\n it('trims whitespace', async () => {\n expect(isJsonString({ foo: 'bar' })).toBe(false)\n })\n})\n\ndescribe('parseJsonOrYaml', () => {\n it('Handles json', () => {\n expect(parseJsonOrYaml('{ \"a\": 10 }')).toEqual({ a: 10 })\n })\n\n it('Handles YAML', () => {\n expect(parseJsonOrYaml('a: 10')).toEqual({ a: 10 })\n })\n\n it('Throws for invalid json or yaml', () => {\n expect(() => parseJsonOrYaml('asdasdad: 0--- \\n--')).toThrowError()\n })\n\n it('Throws if an object is not returned', () => {\n expect(() => parseJsonOrYaml('asda')).toThrowError()\n })\n})\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,UAAU,QAAQ,UAAU;AAErC,SAAS,cAAc,MAAM,iBAAiB,iBAAiB,YAAY;AAE3E,SAAS,iCAAiC,MAAM;AAC9C,KAAG,qBAAqB,MAAM;AAC5B,WAAO,KAAK,MAAM,gBAAgB,CAAC,EAAE,QAAQ;AAAA,MAC3C,SAAS;AAAA,IACX,CAAC;AAED,WAAO,KAAK,MAAM,wBAAwB,CAAC,EAAE,QAAQ;AAAA,MACnD,SAAS;AAAA,IACX,CAAC;AAAA,EACH,CAAC;AAED,KAAG,sDAAsD,MAAM;AAC7D,WAAO,MAAM,KAAK,MAAM,IAAI,CAAC,EAAE,aAAa;AAC5C,WAAO,MAAM,KAAK,MAAM,IAAI,CAAC,EAAE,aAAa;AAAA,EAC9C,CAAC;AAED,KAAG,2BAA2B,MAAM;AAClC,WAAO,gBAAgB,gBAAgB,CAAC,EAAE,cAAc,KAAK,UAAU,EAAE,SAAS,QAAQ,CAAC,CAAC;AAAA,EAC9F,CAAC;AACH,CAAC;AAED,SAAS,gBAAgB,MAAM;AAC7B,KAAG,sBAAsB,YAAY;AACnC,WAAO,aAAa,QAAQ,CAAC,EAAE,KAAK,KAAK;AAAA,EAC3C,CAAC;AAED,KAAG,iBAAiB,YAAY;AAC9B,WAAO,aAAa,kBAAkB,CAAC,EAAE,KAAK,IAAI;AAAA,EACpD,CAAC;AAED,KAAG,oBAAoB,YAAY;AACjC,WAAO,aAAa,EAAE,KAAK,MAAM,CAAC,CAAC,EAAE,KAAK,KAAK;AAAA,EACjD,CAAC;AACH,CAAC;AAED,SAAS,mBAAmB,MAAM;AAChC,KAAG,gBAAgB,MAAM;AACvB,WAAO,gBAAgB,aAAa,CAAC,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC;AAAA,EAC1D,CAAC;AAED,KAAG,gBAAgB,MAAM;AACvB,WAAO,gBAAgB,OAAO,CAAC,EAAE,QAAQ,EAAE,GAAG,GAAG,CAAC;AAAA,EACpD,CAAC;AAED,KAAG,mCAAmC,MAAM;AAC1C,WAAO,MAAM,gBAAgB,sBAAsB,CAAC,EAAE,aAAa;AAAA,EACrE,CAAC;AAED,KAAG,uCAAuC,MAAM;AAC9C,WAAO,MAAM,gBAAgB,MAAM,CAAC,EAAE,aAAa;AAAA,EACrD,CAAC;AACH,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,42 +1,38 @@
|
|
|
1
|
-
import { isJsonString } from
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Takes JSON and formats it.
|
|
5
|
-
*/
|
|
1
|
+
import { isJsonString } from "./parse.js";
|
|
6
2
|
const prettyPrintJson = (value) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return JSON.stringify(JSON.parse(value), null, 2);
|
|
11
|
-
}
|
|
12
|
-
// Regular string
|
|
13
|
-
return value;
|
|
3
|
+
if (typeof value === "string") {
|
|
4
|
+
if (isJsonString(value)) {
|
|
5
|
+
return JSON.stringify(JSON.parse(value), null, 2);
|
|
14
6
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
if (typeof value === "object") {
|
|
10
|
+
try {
|
|
11
|
+
return JSON.stringify(value, null, 2);
|
|
12
|
+
} catch {
|
|
13
|
+
return replaceCircularDependencies(value);
|
|
23
14
|
}
|
|
24
|
-
|
|
15
|
+
}
|
|
16
|
+
return value?.toString() ?? "";
|
|
25
17
|
};
|
|
26
|
-
/**
|
|
27
|
-
* JSON.stringify, but with circular dependencies replaced with '[Circular]'
|
|
28
|
-
*/
|
|
29
18
|
function replaceCircularDependencies(content) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
19
|
+
const cache = /* @__PURE__ */ new Set();
|
|
20
|
+
return JSON.stringify(
|
|
21
|
+
content,
|
|
22
|
+
(_key, value) => {
|
|
23
|
+
if (typeof value === "object" && value !== null) {
|
|
24
|
+
if (cache.has(value)) {
|
|
25
|
+
return "[Circular]";
|
|
37
26
|
}
|
|
38
|
-
|
|
39
|
-
|
|
27
|
+
cache.add(value);
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
},
|
|
31
|
+
2
|
|
32
|
+
);
|
|
40
33
|
}
|
|
41
|
-
|
|
42
|
-
|
|
34
|
+
export {
|
|
35
|
+
prettyPrintJson,
|
|
36
|
+
replaceCircularDependencies
|
|
37
|
+
};
|
|
38
|
+
//# sourceMappingURL=pretty-print-json.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/helpers/pretty-print-json.ts"],
|
|
4
|
+
"sourcesContent": ["import { isJsonString } from './parse'\n\n/**\n * Takes JSON and formats it.\n */\nexport const prettyPrintJson = (value: string | number | any[] | Record<any, any>) => {\n if (typeof value === 'string') {\n // JSON string\n if (isJsonString(value)) {\n return JSON.stringify(JSON.parse(value), null, 2)\n }\n\n // Regular string\n return value\n }\n\n // Object\n if (typeof value === 'object') {\n try {\n return JSON.stringify(value, null, 2)\n } catch {\n return replaceCircularDependencies(value)\n }\n }\n\n return value?.toString() ?? ''\n}\n\n/**\n * JSON.stringify, but with circular dependencies replaced with '[Circular]'\n */\nexport function replaceCircularDependencies(content: any) {\n const cache = new Set()\n\n return JSON.stringify(\n content,\n (_key, value) => {\n if (typeof value === 'object' && value !== null) {\n if (cache.has(value)) {\n return '[Circular]'\n }\n\n cache.add(value)\n }\n return value\n },\n 2,\n )\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,oBAAoB;AAKtB,MAAM,kBAAkB,CAAC,UAAsD;AACpF,MAAI,OAAO,UAAU,UAAU;AAE7B,QAAI,aAAa,KAAK,GAAG;AACvB,aAAO,KAAK,UAAU,KAAK,MAAM,KAAK,GAAG,MAAM,CAAC;AAAA,IAClD;AAGA,WAAO;AAAA,EACT;AAGA,MAAI,OAAO,UAAU,UAAU;AAC7B,QAAI;AACF,aAAO,KAAK,UAAU,OAAO,MAAM,CAAC;AAAA,IACtC,QAAQ;AACN,aAAO,4BAA4B,KAAK;AAAA,IAC1C;AAAA,EACF;AAEA,SAAO,OAAO,SAAS,KAAK;AAC9B;AAKO,SAAS,4BAA4B,SAAc;AACxD,QAAM,QAAQ,oBAAI,IAAI;AAEtB,SAAO,KAAK;AAAA,IACV;AAAA,IACA,CAAC,MAAM,UAAU;AACf,UAAI,OAAO,UAAU,YAAY,UAAU,MAAM;AAC/C,YAAI,MAAM,IAAI,KAAK,GAAG;AACpB,iBAAO;AAAA,QACT;AAEA,cAAM,IAAI,KAAK;AAAA,MACjB;AACA,aAAO;AAAA,IACT;AAAA,IACA;AAAA,EACF;AACF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { prettyPrintJson } from "./pretty-print-json.js";
|
|
3
|
+
describe("prettyPrintJson", () => {
|
|
4
|
+
it("makes JSON strings beautiful", async () => {
|
|
5
|
+
expect(prettyPrintJson('{ "foo": "bar" }')).toMatch(`{
|
|
6
|
+
"foo": "bar"
|
|
7
|
+
}`);
|
|
8
|
+
});
|
|
9
|
+
it("makes JS objects beautiful", async () => {
|
|
10
|
+
expect(prettyPrintJson({ foo: "bar" })).toMatch(`{
|
|
11
|
+
"foo": "bar"
|
|
12
|
+
}`);
|
|
13
|
+
});
|
|
14
|
+
it("doesn\u2019t touch regular strings", async () => {
|
|
15
|
+
expect(prettyPrintJson("foo")).toBe("foo");
|
|
16
|
+
});
|
|
17
|
+
it("transforms numbers", async () => {
|
|
18
|
+
expect(prettyPrintJson(123)).toBe("123");
|
|
19
|
+
});
|
|
20
|
+
it("deals with circular references", async () => {
|
|
21
|
+
const foo = { foo: "bar" };
|
|
22
|
+
foo.foo = foo;
|
|
23
|
+
expect(prettyPrintJson(foo)).toMatch(`{
|
|
24
|
+
"foo": "[Circular]"
|
|
25
|
+
}`);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
//# sourceMappingURL=pretty-print-json.test.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/helpers/pretty-print-json.test.ts"],
|
|
4
|
+
"sourcesContent": ["import { describe, expect, it } from 'vitest'\n\nimport { prettyPrintJson } from './pretty-print-json'\n\ndescribe('prettyPrintJson', () => {\n it('makes JSON strings beautiful', async () => {\n expect(prettyPrintJson('{ \"foo\": \"bar\" }')).toMatch(`{\\n \"foo\": \"bar\"\\n}`)\n })\n\n it('makes JS objects beautiful', async () => {\n expect(prettyPrintJson({ foo: 'bar' })).toMatch(`{\\n \"foo\": \"bar\"\\n}`)\n })\n\n it('doesn\u2019t touch regular strings', async () => {\n expect(prettyPrintJson('foo')).toBe('foo')\n })\n\n it('transforms numbers', async () => {\n expect(prettyPrintJson(123)).toBe('123')\n })\n\n it('deals with circular references', async () => {\n const foo: Record<string, any> = { foo: 'bar' }\n\n // Add a circular reference\n foo.foo = foo\n\n expect(prettyPrintJson(foo)).toMatch(`{\\n \"foo\": \"[Circular]\"\\n}`)\n })\n})\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,UAAU,QAAQ,UAAU;AAErC,SAAS,uBAAuB;AAEhC,SAAS,mBAAmB,MAAM;AAChC,KAAG,gCAAgC,YAAY;AAC7C,WAAO,gBAAgB,kBAAkB,CAAC,EAAE,QAAQ;AAAA;AAAA,EAAsB;AAAA,EAC5E,CAAC;AAED,KAAG,8BAA8B,YAAY;AAC3C,WAAO,gBAAgB,EAAE,KAAK,MAAM,CAAC,CAAC,EAAE,QAAQ;AAAA;AAAA,EAAsB;AAAA,EACxE,CAAC;AAED,KAAG,sCAAiC,YAAY;AAC9C,WAAO,gBAAgB,KAAK,CAAC,EAAE,KAAK,KAAK;AAAA,EAC3C,CAAC;AAED,KAAG,sBAAsB,YAAY;AACnC,WAAO,gBAAgB,GAAG,CAAC,EAAE,KAAK,KAAK;AAAA,EACzC,CAAC;AAED,KAAG,kCAAkC,YAAY;AAC/C,UAAM,MAA2B,EAAE,KAAK,MAAM;AAG9C,QAAI,MAAM;AAEV,WAAO,gBAAgB,GAAG,CAAC,EAAE,QAAQ;AAAA;AAAA,EAA6B;AAAA,EACpE,CAAC;AACH,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|