Spaces:
Sleeping
Sleeping
| diff --git a/dist/index.js b/dist/index.js | |
| index 716afaa50ea30a433178843def931d6d1c4fbe33..28c9250a19081e0569b1de30662510eef90cd8e9 100644 | |
| --- a/dist/index.js | |
| +++ b/dist/index.js | |
| async function openapiTS(schema, options = {}) { | |
| if (options.inject) | |
| output.push(options.inject); | |
| const rootTypes = transformSchema(allSchemas["."].schema, ctx); | |
| + const genericParams = "<TMode extends 'read' | 'write' | 'none' = 'none'>"; | |
| + | |
| for (const k of Object.keys(rootTypes)) { | |
| if (rootTypes[k] && !EMPTY_OBJECT_RE.test(rootTypes[k])) { | |
| - output.push(options.exportType ? `export type ${k} = ${rootTypes[k]};` : `export interface ${k} ${rootTypes[k]}`, ""); | |
| + if (k === "paths" || k === "webhooks" || k === "$defs") { | |
| + output.push(options.exportType | |
| + ? `export type ${k} = ${rootTypes[k]};` | |
| + : `export interface ${k} ${rootTypes[k]}`, ""); | |
| + } else { | |
| + output.push(options.exportType | |
| + ? `export type ${k}${genericParams} = ${rootTypes[k]};` | |
| + : `export interface ${k}${genericParams} ${rootTypes[k]}`, ""); | |
| + } | |
| } | |
| else { | |
| output.push(`export type ${k} = Record<string, never>;`, ""); | |
| async function openapiTS(schema, options = {}) { | |
| output.push(indent(`}${options.exportType ? ";" : ""}`, indentLv), ""); | |
| } | |
| else { | |
| - output.push(`export type external = Record<string, never>;`, ""); | |
| + output.push("export type external = Record<string, never>;", ""); | |
| } | |
| if (Object.keys(ctx.operations).length) { | |
| output.push(options.exportType ? "export type operations = {" : "export interface operations {", ""); | |
| async function openapiTS(schema, options = {}) { | |
| output.push(`}${options.exportType ? ";" : ""}`, ""); | |
| } | |
| else { | |
| - output.push(`export type operations = Record<string, never>;`, ""); | |
| + output.push("export type operations = Record<string, never>;", ""); | |
| } | |
| if (output.join("\n").includes("OneOf")) { | |
| output.splice(1, 0, "/** OneOf type helpers */", "type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };", "type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;", "type OneOf<T extends any[]> = T extends [infer Only] ? Only : T extends [infer A, infer B, ...infer Rest] ? OneOf<[XOR<A, B>, ...Rest]> : never;", ""); | |
| diff --git a/dist/load.js b/dist/load.js | |
| index aea4fe09436722e0c2d1fb272b1e067c17a9af60..69bb601d8d6bcf022acad9f5e9b0221b230d376b 100644 | |
| --- a/dist/load.js | |
| +++ b/dist/load.js | |
| export default async function load(schema, options) { | |
| return options.schemas; | |
| } | |
| options.urlCache.add(schemaID); | |
| - const ext = path.extname(schema.pathname).toLowerCase(); | |
| - if (schema.protocol.startsWith("http")) { | |
| + if (schema.protocol.startsWith("http") || schema.protocol.startsWith("https")) { | |
| const headers = { "User-Agent": "openapi-typescript" }; | |
| if (options.auth) | |
| headers.Authorization = options.auth; | |
| export default async function load(schema, options) { | |
| headers, | |
| }); | |
| const contentType = res.headers.get("content-type"); | |
| + const contentDisposition = res.headers.get("content-disposition"); | |
| + | |
| + const match = contentDisposition.match(/^.*filename="?(?<name>.+?)"?(?:;|$)/); | |
| + const filename = match | |
| + ? match.groups.name | |
| + : undefined; | |
| + | |
| + const ext = path.extname(filename).toLowerCase(); | |
| if (ext === ".json" || contentType?.includes("json")) { | |
| options.schemas[schemaID] = { | |
| hint, | |
| export default async function load(schema, options) { | |
| } | |
| } | |
| else { | |
| + const ext = path.extname(schema.pathname).toLowerCase(); | |
| const contents = fs.readFileSync(schema, "utf8"); | |
| if (ext === ".yaml" || ext === ".yml") { | |
| options.schemas[schemaID] = { | |
| diff --git a/dist/transform/request-body-object.js b/dist/transform/request-body-object.js | |
| index 33c24bea962dfa595dc0aa474d7826f5e3cfb7c8..5645d67d675a18a44d5565c360a5ac4f846d1b12 100644 | |
| --- a/dist/transform/request-body-object.js | |
| +++ b/dist/transform/request-body-object.js | |
| export default function transformRequestBodyObject(requestBodyObject, { path, ct | |
| if ("$ref" in mediaTypeObject) { | |
| output.push(indent(`${key}: ${transformSchemaObject(mediaTypeObject, { | |
| path: `${path}/${contentType}`, | |
| - ctx: { ...ctx, indentLv }, | |
| + ctx: { ...ctx, indentLv, mode: "write" }, | |
| })};`, indentLv)); | |
| } | |
| else { | |
| const mediaType = transformMediaTypeObject(mediaTypeObject, { | |
| path: `${path}/${contentType}`, | |
| - ctx: { ...ctx, indentLv }, | |
| + ctx: { ...ctx, indentLv, mode: "write" }, | |
| }); | |
| output.push(indent(`${key}: ${mediaType};`, indentLv)); | |
| } | |
| diff --git a/dist/transform/response-object.js b/dist/transform/response-object.js | |
| index b0e1c203a3d2030dd40b5435419dfc6a360dc546..a4fb5b6b226618efc707568c028abe3558a57800 100644 | |
| --- a/dist/transform/response-object.js | |
| +++ b/dist/transform/response-object.js | |
| export default function transformResponseObject(responseObject, { path, ctx }) { | |
| key = tsOptionalProperty(key); | |
| output.push(indent(`${key}: ${transformHeaderObject(headerObject, { | |
| path: `${path}/headers/${name}`, | |
| - ctx: { ...ctx, indentLv }, | |
| + ctx: { ...ctx, indentLv, mode: "read" }, | |
| })};`, indentLv)); | |
| } | |
| } | |
| export default function transformResponseObject(responseObject, { path, ctx }) { | |
| key = tsReadonly(key); | |
| output.push(indent(`${key}: ${transformMediaTypeObject(mediaTypeObject, { | |
| path: `${path}/content/${contentType}`, | |
| - ctx: { ...ctx, indentLv: indentLv }, | |
| + ctx: { ...ctx, indentLv: indentLv, mode: "read" }, | |
| })};`, indentLv)); | |
| } | |
| indentLv--; | |
| diff --git a/dist/transform/schema-object.js b/dist/transform/schema-object.js | |
| index 15f144cc7258250559139582ff57f4bb5816aae3..9597ee66f76071a292327abd1f004410e0ea620a 100644 | |
| --- a/dist/transform/schema-object.js | |
| +++ b/dist/transform/schema-object.js | |
| export function defaultSchemaObjectTransform(schemaObject, { path, ctx }) { | |
| return ctx.immutableTypes ? tsReadonly(finalType) : finalType; | |
| } | |
| if ("$ref" in schemaObject) { | |
| - return schemaObject.$ref; | |
| + const genericValue = ctx.mode ? `'${ctx.mode}'` : "TMode"; | |
| + return schemaObject.$ref.replace(/^(\w+)/, `$1<${genericValue}>`); | |
| } | |
| if (typeof ctx.transform === "function") { | |
| const result = ctx.transform(schemaObject, { path, ctx }); | |
| if (result) | |
| return result; | |
| } | |
| + | |
| + if (schemaObject.readOnly) { | |
| + return `TMode extends 'write' ? never : ${defaultSchemaObjectTransform( | |
| + { ...schemaObject, readOnly: undefined }, | |
| + { path, ctx } | |
| + )}`; | |
| + } | |
| + | |
| + if (schemaObject.writeOnly) { | |
| + return `TMode extends 'read' ? never : ${defaultSchemaObjectTransform( | |
| + { ...schemaObject, writeOnly: undefined }, | |
| + { path, ctx } | |
| + )}`; | |
| + } | |
| + | |
| if (schemaObject.const !== null && schemaObject.const !== undefined) { | |
| return transformSchemaObject(escStr(schemaObject.const), { | |
| path, | |
| diff --git a/dist/types.d.ts b/dist/types.d.ts | |
| index 9b28fc0323dae76ae04f5fcfb1e2529650d3444e..732a78968f75de19b914f9cf6015d6e9676f5073 100644 | |
| --- a/dist/types.d.ts | |
| +++ b/dist/types.d.ts | |
| export interface GlobalContext { | |
| silent: boolean; | |
| supportArrayLength: boolean; | |
| excludeDeprecated: boolean; | |
| + mode?: "read" | "write"; | |
| } | |
| export type $defs = Record<string, SchemaObject>; | |
| export type Fetch = (input: RequestInfo, init?: RequestInit) => Promise<Response>; |