@automatons/tools 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,3 +3,5 @@
3
3
  ## What @automatons/tools
4
4
  This is utils for openapi-automatons.
5
5
  Only use openapi-automatons.
6
+
7
+ Since v2 this package is **ESM-only** and requires **Node.js >= 22**.
package/index.d.ts CHANGED
@@ -31,6 +31,7 @@ interface OpenapiInfoLicense {
31
31
  }
32
32
  interface OpenapiServer {
33
33
  url: string;
34
+ name?: string;
34
35
  'x-name'?: string;
35
36
  description?: string;
36
37
  variables?: OpenapiMap<OpenapiServerVariable>;
@@ -170,7 +171,7 @@ interface OpenapiComponents {
170
171
  links?: object;
171
172
  callbacks?: object;
172
173
  }
173
- type OpenapiSchema = OpenapiSchemaString | OpenapiSchemaBoolean | OpenapiSchemaNumber | OpenapiSchemaInteger | OpenapiSchemaObject | OpenapiSchemaArray | OpenapiSchemaAllOf | OpenapiSchemaOneOf | OpenapiSchemaReference;
174
+ type OpenapiSchema = OpenapiSchemaString | OpenapiSchemaBoolean | OpenapiSchemaNumber | OpenapiSchemaInteger | OpenapiSchemaObject | OpenapiSchemaArray | OpenapiSchemaAllOf | OpenapiSchemaOneOf | OpenapiSchemaAnyOf | OpenapiSchemaReference;
174
175
  type OpenapiSchemaReference = OpenapiReference & OpenapiSchemaCommon;
175
176
  interface OpenapiSchemaString extends OpenapiSchemaCommon {
176
177
  type: 'string';
@@ -233,6 +234,15 @@ interface OpenapiSchemaOneOf extends OpenapiSchemaCommon {
233
234
  };
234
235
  };
235
236
  }
237
+ interface OpenapiSchemaAnyOf extends OpenapiSchemaCommon {
238
+ anyOf: OpenapiSchema[];
239
+ discriminator?: {
240
+ propertyName: string;
241
+ mapping?: {
242
+ [key: string]: string;
243
+ };
244
+ };
245
+ }
236
246
  interface OpenapiSchemaCommon {
237
247
  title?: string;
238
248
  description?: string;
@@ -324,6 +334,7 @@ declare const isSchemaObject: (type: OpenapiSchema) => type is OpenapiSchemaObje
324
334
  declare const isSchemaArray: (type: OpenapiSchema) => type is OpenapiSchemaArray;
325
335
  declare const isSchemaAllOf: (type: OpenapiSchema) => type is OpenapiSchemaAllOf;
326
336
  declare const isSchemaOneOf: (type: OpenapiSchema) => type is OpenapiSchemaOneOf;
337
+ declare const isSchemaAnyOf: (type: OpenapiSchema) => type is OpenapiSchemaAnyOf;
327
338
  declare const isSchemaRef: (type: OpenapiSchema) => type is OpenapiSchemaReference;
328
339
 
329
340
  declare const isUrl: (url: string) => boolean;
@@ -338,4 +349,4 @@ type AutomatonSetting = {
338
349
  options?: object;
339
350
  };
340
351
 
341
- export { type Automaton, type AutomatonContext, type AutomatonSetting, type AutomatonSettings, HTTP_METHODS, type Method, type Openapi, type OpenapiComponents, type OpenapiExample, type OpenapiExternalDocument, type OpenapiInfo, type OpenapiInfoContact, type OpenapiInfoLicense, type OpenapiMap, type OpenapiParameter, type OpenapiParameterCommon, type OpenapiParameterCookie, type OpenapiParameterHeader, type OpenapiParameterPath, type OpenapiParameterQuery, type OpenapiPath, type OpenapiPathCommon, type OpenapiPathMedia, type OpenapiPathOperation, type OpenapiPathRequestBody, type OpenapiPathResponse, type OpenapiPathResponseHeader, type OpenapiPathResponseLink, type OpenapiPathResponseMediaEncoding, type OpenapiReference, type OpenapiSchema, type OpenapiSchemaAllOf, type OpenapiSchemaArray, type OpenapiSchemaBoolean, type OpenapiSchemaCommon, type OpenapiSchemaInteger, type OpenapiSchemaNumber, type OpenapiSchemaObject, type OpenapiSchemaOneOf, type OpenapiSchemaReference, type OpenapiSchemaString, type OpenapiSecurity, type OpenapiSecurityApiKeySchema, type OpenapiSecurityHttpBearerSchema, type OpenapiSecurityHttpSchema, type OpenapiSecurityOAuth2Schema, type OpenapiSecurityOAuthFlow, type OpenapiSecurityOpenIdConnectSchema, type OpenapiSecuritySchema, type OpenapiServer, type OpenapiServerVariable, type OpenapiTag, type Setting, fetch, hasContent, hasSchema, isCookieParam, isHeaderParam, isPathParam, isQueryParam, isRef, isSchemaAllOf, isSchemaArray, isSchemaBoolean, isSchemaInteger, isSchemaNumber, isSchemaObject, isSchemaOneOf, isSchemaRef, isSchemaString, isUrl, referenceSchema, referenceTitle };
352
+ export { type Automaton, type AutomatonContext, type AutomatonSetting, type AutomatonSettings, HTTP_METHODS, type Method, type Openapi, type OpenapiComponents, type OpenapiExample, type OpenapiExternalDocument, type OpenapiInfo, type OpenapiInfoContact, type OpenapiInfoLicense, type OpenapiMap, type OpenapiParameter, type OpenapiParameterCommon, type OpenapiParameterCookie, type OpenapiParameterHeader, type OpenapiParameterPath, type OpenapiParameterQuery, type OpenapiPath, type OpenapiPathCommon, type OpenapiPathMedia, type OpenapiPathOperation, type OpenapiPathRequestBody, type OpenapiPathResponse, type OpenapiPathResponseHeader, type OpenapiPathResponseLink, type OpenapiPathResponseMediaEncoding, type OpenapiReference, type OpenapiSchema, type OpenapiSchemaAllOf, type OpenapiSchemaAnyOf, type OpenapiSchemaArray, type OpenapiSchemaBoolean, type OpenapiSchemaCommon, type OpenapiSchemaInteger, type OpenapiSchemaNumber, type OpenapiSchemaObject, type OpenapiSchemaOneOf, type OpenapiSchemaReference, type OpenapiSchemaString, type OpenapiSecurity, type OpenapiSecurityApiKeySchema, type OpenapiSecurityHttpBearerSchema, type OpenapiSecurityHttpSchema, type OpenapiSecurityOAuth2Schema, type OpenapiSecurityOAuthFlow, type OpenapiSecurityOpenIdConnectSchema, type OpenapiSecuritySchema, type OpenapiServer, type OpenapiServerVariable, type OpenapiTag, type Setting, fetch, hasContent, hasSchema, isCookieParam, isHeaderParam, isPathParam, isQueryParam, isRef, isSchemaAllOf, isSchemaAnyOf, isSchemaArray, isSchemaBoolean, isSchemaInteger, isSchemaNumber, isSchemaObject, isSchemaOneOf, isSchemaRef, isSchemaString, isUrl, referenceSchema, referenceTitle };
package/index.js CHANGED
@@ -96,6 +96,7 @@ var isSchemaObject = (type) => type.type === "object" || Object.hasOwn(type, "pr
96
96
  var isSchemaArray = (type) => type.type === "array" || Object.hasOwn(type, "items");
97
97
  var isSchemaAllOf = (type) => Object.hasOwn(type, "allOf");
98
98
  var isSchemaOneOf = (type) => Object.hasOwn(type, "oneOf");
99
+ var isSchemaAnyOf = (type) => Object.hasOwn(type, "anyOf");
99
100
  var isSchemaRef = (type) => Object.hasOwn(type, "$ref");
100
101
  export {
101
102
  HTTP_METHODS,
@@ -108,6 +109,7 @@ export {
108
109
  isQueryParam,
109
110
  isRef,
110
111
  isSchemaAllOf,
112
+ isSchemaAnyOf,
111
113
  isSchemaArray,
112
114
  isSchemaBoolean,
113
115
  isSchemaInteger,
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/values/http.ts","../src/utils/fetch.ts","../src/utils/url.ts","../src/utils/openapi.ts","../src/utils/parameter.ts","../src/utils/reference.ts","../src/utils/schema.ts"],"sourcesContent":["import {Method} from '../types';\n\nexport const HTTP_METHODS: Method[] = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];\n","import {readFile} from 'node:fs/promises';\nimport {dirname, extname, resolve} from 'node:path';\nimport {load} from 'js-yaml';\nimport {Openapi} from '../types';\nimport {isUrl} from './url';\n\nexport const fetch = async <T = Openapi>(url: string, openapiPath?: string): Promise<T> =>\n parse(await read(url, openapiPath), url);\n\nconst read = async (url: string, openapiPath?: string): Promise<string> => {\n if (isUrl(url)) {\n return fetchText(url);\n }\n if (openapiPath && isUrl(openapiPath)) {\n return fetchText(new URL(url, openapiPath).toString());\n }\n const base = openapiPath ? (extname(openapiPath) ? dirname(openapiPath) : openapiPath) : '';\n return readFile(resolve(base, url), {encoding: 'utf-8'});\n};\n\nconst fetchText = async (url: string): Promise<string> => {\n const response = await globalThis.fetch(url);\n if (!response.ok) {\n throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);\n }\n return response.text();\n};\n\nconst parse = <T = Openapi>(data: string, filePath: string): T => {\n switch (extname(filePath)) {\n case '.json':\n return JSON.parse(data);\n case '.yml':\n case '.yaml': {\n const yaml = load(data);\n if (typeof yaml === 'object') {\n return yaml as T;\n }\n throw new Error('Unsupported file format');\n }\n default:\n throw new Error('Unsupported file extension');\n }\n};\n","export const isUrl = (url: string) =>\n url.startsWith('http://') || url.startsWith('https://');\n","import {OpenapiReference} from '../types';\n\nexport const isRef = (schema: any): schema is OpenapiReference => Object.hasOwn(schema, '$ref');\n","import {\n OpenapiMap,\n OpenapiParameter,\n OpenapiParameterCookie,\n OpenapiParameterHeader,\n OpenapiParameterPath,\n OpenapiParameterQuery,\n OpenapiPathMedia,\n OpenapiSchema,\n} from '../types';\nimport {isRef} from './openapi';\n\nexport const hasSchema =\n (param: OpenapiParameter): param is OpenapiParameter & { schema: OpenapiSchema } =>\n Object.hasOwn(param, 'schema');\nexport const hasContent =\n (param: OpenapiParameter): param is OpenapiParameter & { content: OpenapiMap<OpenapiPathMedia> } =>\n Object.hasOwn(param, 'content');\nexport const isPathParam =\n (param: OpenapiParameter): param is OpenapiParameterPath => !isRef(param) && param.in === 'path';\nexport const isQueryParam =\n (param: OpenapiParameter): param is OpenapiParameterQuery => !isRef(param) && param.in === 'query';\nexport const isHeaderParam =\n (param: OpenapiParameter): param is OpenapiParameterHeader => !isRef(param) && param.in === 'header';\nexport const isCookieParam =\n (param: OpenapiParameter): param is OpenapiParameterCookie => !isRef(param) && param.in === 'cookie';\n","import {parse} from 'path';\nimport {AutomatonContext, OpenapiReference} from '../types';\nimport {fetch} from './fetch';\nimport {isRef} from './openapi';\nimport {isUrl} from './url';\n\nexport const referenceTitle = (schema: OpenapiReference): string => {\n const [url = '', path = ''] = schema.$ref.split('#');\n const paths = path.split('/').slice(1);\n if (paths.length) {\n return paths[paths.length - 1]!;\n } else if (isUrl(url)) {\n return parse(url).name;\n }\n throw new Error(`Invalid ref format: ${schema.$ref}\\n Can not extract name.`);\n};\n\ntype ReferenceSchema<T = unknown> = (T | OpenapiReference) | (T & OpenapiReference);\nexport const referenceSchema =\n async <T = unknown>(schema: ReferenceSchema<T>, {openapi, settings: {openapiPath}}: AutomatonContext): Promise<T> => {\n if (!isRef(schema)) return schema;\n const [url, path] = schema.$ref.split('#');\n const file = url ? await fetch<T>(url, openapiPath) : openapi;\n if (!path) {\n if (!file) {\n throw new Error(`Invalid ref path ${schema.$ref}`);\n }\n return file as T;\n }\n const paths = path?.split('/').slice(1);\n const extractSchema = paths?.reduce((pre: any, cur) => pre?.[cur], file) as T | undefined;\n if (!extractSchema) {\n throw new Error(`Invalid ref path ${schema.$ref}`);\n }\n return extractSchema;\n };\n","import {\n OpenapiSchema,\n OpenapiSchemaAllOf,\n OpenapiSchemaArray,\n OpenapiSchemaBoolean,\n OpenapiSchemaInteger,\n OpenapiSchemaNumber,\n OpenapiSchemaObject,\n OpenapiSchemaOneOf,\n OpenapiSchemaReference,\n OpenapiSchemaString,\n} from '../types';\n\nexport const isSchemaString =\n (type: OpenapiSchema): type is OpenapiSchemaString => (type as OpenapiSchemaString).type === 'string';\nexport const isSchemaNumber =\n (type: OpenapiSchema): type is OpenapiSchemaNumber => (type as OpenapiSchemaNumber).type === 'number';\nexport const isSchemaInteger =\n (type: OpenapiSchema): type is OpenapiSchemaInteger => (type as OpenapiSchemaInteger).type === 'integer';\nexport const isSchemaBoolean =\n (type: OpenapiSchema): type is OpenapiSchemaBoolean => (type as OpenapiSchemaBoolean).type === 'boolean';\nexport const isSchemaObject =\n (type: OpenapiSchema): type is OpenapiSchemaObject =>\n (type as OpenapiSchemaObject).type === 'object' || Object.hasOwn(type, 'properties');\nexport const isSchemaArray =\n (type: OpenapiSchema): type is OpenapiSchemaArray =>\n (type as OpenapiSchemaArray).type === 'array' || Object.hasOwn(type, 'items');\nexport const isSchemaAllOf =\n (type: OpenapiSchema): type is OpenapiSchemaAllOf => Object.hasOwn(type, 'allOf');\nexport const isSchemaOneOf =\n (type: OpenapiSchema): type is OpenapiSchemaOneOf => Object.hasOwn(type, 'oneOf');\nexport const isSchemaRef =\n (type: Openapi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A,IACnD;AACA,WAAO;AAAA,EACT;AACA,QAAM,QAAQ,MAAM,MAAM,GAAG,EAAE,MAAM,CAAC;AACtC,QAAM,gBAAgB,OAAO,OAAO,CAAC,KAAU,QAAQ,MAAM,GAAG,GAAG,IAAI;AACvE,MAAI,CAAC,eAAe;AAClB,UAAM,IAAI,MAAM,oBAAoB,OAAO,IAAI,EAAE;AAAA,EACnD;AACA,SAAO;AACT;;;ACtBK,IAAM,iBACX,CAAC,SAAsD,KAA6B,SAAS;AACxF,IAAM,iBACX,CAAC,SAAsD,KAA6B,SAAS;AACxF,IAAM,kBACX,CAAC,SAAuD,KAA8B,SAAS;AAC1F,IAAM,kBACX,CAAC,SAAuD,KAA8B,SAAS;AAC1F,IAAM,iBACX,CAAC,SACE,KAA6B,SAAS,YAAY,OAAO,OAAO,MAAM,YAAY;AAChF,IAAM,gBACX,CAAC,SACE,KAA4B,SAAS,WAAW,OAAO,OAAO,MAAM,OAAO;AACzE,IAAM,gBACX,CAAC,SAAoD,OAAO,OAAO,MAAM,OAAO;AAC3E,IAAM,gBACX,CAAC,SAAoD,OAAO,OAAO,MAAM,OAAO;AAC3E,IAAM,cACX,CAAC,SAAwD,OAAO,OAAO,MAAM,MAAM;","names":["parse","parse"]}
1
+ {"version":3,"sources":["../src/values/http.ts","../src/utils/fetch.ts","../src/utils/url.ts","../src/utils/openapi.ts","../src/utils/parameter.ts","../src/utils/reference.ts","../src/utils/schema.ts"],"sourcesContent":["import {Method} from '../types';\n\nexport const HTTP_METHODS: Method[] = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'];\n","import {readFile} from 'node:fs/promises';\nimport {dirname, extname, resolve} from 'node:path';\nimport {load} from 'js-yaml';\nimport {Openapi} from '../types';\nimport {isUrl} from './url';\n\nexport const fetch = async <T = Openapi>(url: string, openapiPath?: string): Promise<T> =>\n parse(await read(url, openapiPath), url);\n\nconst read = async (url: string, openapiPath?: string): Promise<string> => {\n if (isUrl(url)) {\n return fetchText(url);\n }\n if (openapiPath && isUrl(openapiPath)) {\n return fetchText(new URL(url, openapiPath).toString());\n }\n const base = openapiPath ? (extname(openapiPath) ? dirname(openapiPath) : openapiPath) : '';\n return readFile(resolve(base, url), {encoding: 'utf-8'});\n};\n\nconst fetchText = async (url: string): Promise<string> => {\n const response = await globalThis.fetch(url);\n if (!response.ok) {\n throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);\n }\n return response.text();\n};\n\nconst parse = <T = Openapi>(data: string, filePath: string): T => {\n switch (extname(filePath)) {\n case '.json':\n return JSON.parse(data);\n case '.yml':\n case '.yaml': {\n const yaml = load(data);\n if (typeof yaml === 'object') {\n return yaml as T;\n }\n throw new Error('Unsupported file format');\n }\n default:\n throw new Error('Unsupported file extension');\n }\n};\n","export const isUrl = (url: string) =>\n url.startsWith('http://') || url.startsWith('https://');\n","import {OpenapiReference} from '../types';\n\nexport const isRef = (schema: any): schema is OpenapiReference => Object.hasOwn(schema, '$ref');\n","import {\n OpenapiMap,\n OpenapiParameter,\n OpenapiParameterCookie,\n OpenapiParameterHeader,\n OpenapiParameterPath,\n OpenapiParameterQuery,\n OpenapiPathMedia,\n OpenapiSchema,\n} from '../types';\nimport {isRef} from './openapi';\n\nexport const hasSchema =\n (param: OpenapiParameter): param is OpenapiParameter & { schema: OpenapiSchema } =>\n Object.hasOwn(param, 'schema');\nexport const hasContent =\n (param: OpenapiParameter): param is OpenapiParameter & { content: OpenapiMap<OpenapiPathMedia> } =>\n Object.hasOwn(param, 'content');\nexport const isPathParam =\n (param: OpenapiParameter): param is OpenapiParameterPath => !isRef(param) && param.in === 'path';\nexport const isQueryParam =\n (param: OpenapiParameter): param is OpenapiParameterQuery => !isRef(param) && param.in === 'query';\nexport const isHeaderParam =\n (param: OpenapiParameter): param is OpenapiParameterHeader => !isRef(param) && param.in === 'header';\nexport const isCookieParam =\n (param: OpenapiParameter): param is OpenapiParameterCookie => !isRef(param) && param.in === 'cookie';\n","import {parse} from 'path';\nimport {AutomatonContext, OpenapiReference} from '../types';\nimport {fetch} from './fetch';\nimport {isRef} from './openapi';\nimport {isUrl} from './url';\n\nexport const referenceTitle = (schema: OpenapiReference): string => {\n const [url = '', path = ''] = schema.$ref.split('#');\n const paths = path.split('/').slice(1);\n if (paths.length) {\n return paths[paths.length - 1]!;\n } else if (isUrl(url)) {\n return parse(url).name;\n }\n throw new Error(`Invalid ref format: ${schema.$ref}\\n Can not extract name.`);\n};\n\ntype ReferenceSchema<T = unknown> = (T | OpenapiReference) | (T & OpenapiReference);\nexport const referenceSchema =\n async <T = unknown>(schema: ReferenceSchema<T>, {openapi, settings: {openapiPath}}: AutomatonContext): Promise<T> => {\n if (!isRef(schema)) return schema;\n const [url, path] = schema.$ref.split('#');\n const file = url ? await fetch<T>(url, openapiPath) : openapi;\n if (!path) {\n if (!file) {\n throw new Error(`Invalid ref path ${schema.$ref}`);\n }\n return file as T;\n }\n const paths = path?.split('/').slice(1);\n const extractSchema = paths?.reduce((pre: any, cur) => pre?.[cur], file) as T | undefined;\n if (!extractSchema) {\n throw new Error(`Invalid ref path ${schema.$ref}`);\n }\n return extractSchema;\n };\n","import {\n OpenapiSchema,\n OpenapiSchemaAllOf,\n OpenapiSchemaArray,\n OpenapiSchemaBoolean,\n OpenapiSchemaInteger,\n OpenapiSchemaNumber,\n OpenapiSchemaAnyOf,\n OpenapiSchemaObject,\n OpenapiSchemaOneOf,\n OpenapiSchemaReference,\n OpenapiSchemaString,\n} from '../types';\n\nexport const isSchemaString =\n (type: OpenapiSchema): type is OpenapiSchemaString => (type as OpenapiSchemaString).type === 'string';\nexport const isSchemaNumber =\n (type: OpenapiSchema): type is OpenapiSchemaNumber => (type as OpenapiSchemaNumber).type === 'number';\nexport const isSchemaInteger =\n (type: OpenapiSchema): type is OpenapiSchemaInteger => (type as OpenapiSchemaInteger).type === 'integer';\nexport const isSchemaBoolean =\n (type: OpenapiSchema): type is OpenapiSchemaBoolean => (type as OpenapiSchemaBoolean).type === 'boolean';\nexport const isSchemaObject =\n (type: OpenapiSchema): type is OpenapiSchemaObject =>\n (type as OpenapiSchemaObject).type === 'object' || Object.hasOwn(type, 'properties');\nexport const isSchemaArray =\n (type: OpenapiSchema): type is OpenapiSchemaArray =>\n (type as OpenapiSchemaArray).type === 'array' || Object.hasOwn(type, 'items');\nexport const isSchemaAllOf =\n (type: OpenapiSchema): type is OpenapiSchemaAllOf => Object.hasOwn(type, 'allOf');\nexport const isSchemaOneOf =\n (type: OpenapiSchema): type is OpenapiSchemaOneOf => Object.hasOwn(type, 'oneOf');\nexport const isSchemaAnyOf =\n (type: OpenapiSchema): type is OpenapiSchemaAnyOf => Object.hasOwn(type, 'anyOf');\nexport const isSchemaRef =\n (type: OpenapiSchema): type is OpenapiSchemaReference => Object.hasOwn(type, '$ref');\n"],"mappings":";AAEO,IAAM,eAAyB,CAAC,OAAO,OAAO,QAAQ,UAAU,WAAW,QAAQ,SAAS,OAAO;;;ACF1G,SAAQ,gBAAe;AACvB,SAAQ,SAAS,SAAS,eAAc;AACxC,SAAQ,YAAW;;;ACFZ,IAAM,QAAQ,CAAC,QACpB,IAAI,WAAW,SAAS,KAAK,IAAI,WAAW,UAAU;;;ADKjD,IAAM,QAAQ,OAAoB,KAAa,gBACpD,MAAM,MAAM,KAAK,KAAK,WAAW,GAAG,GAAG;AAEzC,IAAM,OAAO,OAAO,KAAa,gBAA0C;AACzE,MAAI,MAAM,GAAG,GAAG;AACd,WAAO,UAAU,GAAG;AAAA,EACtB;AACA,MAAI,eAAe,MAAM,WAAW,GAAG;AACrC,WAAO,UAAU,IAAI,IAAI,KAAK,WAAW,EAAE,SAAS,CAAC;AAAA,EACvD;AACA,QAAM,OAAO,cAAe,QAAQ,WAAW,IAAI,QAAQ,WAAW,IAAI,cAAe;AACzF,SAAO,SAAS,QAAQ,MAAM,GAAG,GAAG,EAAC,UAAU,QAAO,CAAC;AACzD;AAEA,IAAM,YAAY,OAAO,QAAiC;AACxD,QAAM,WAAW,MAAM,WAAW,MAAM,GAAG;A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AO,OAAO,MAAM,MAAM;","names":["parse","parse"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automatons/tools",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "repository": "https://github.com/openapi-automatons/tools.git",
5
5
  "author": "tanmen <yt.prog@gmail.com>",
6
6
  "license": "MIT",