File size: 8,390 Bytes
d7291ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
diff --git a/dist/index.js b/dist/index.js
index 716afaa50ea30a433178843def931d6d1c4fbe33..28c9250a19081e0569b1de30662510eef90cd8e9 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -89,9 +89,19 @@ 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>;`, "");
@@ -187,7 +197,7 @@ 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 {", "");
@@ -199,7 +209,7 @@ 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
@@ -67,8 +67,7 @@ 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;
@@ -83,6 +82,14 @@ 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,
@@ -97,6 +104,7 @@ 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
@@ -20,13 +20,13 @@ 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
@@ -23,7 +23,7 @@ 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));
             }
         }
@@ -41,7 +41,7 @@ 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
@@ -21,13 +21,29 @@ 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
@@ -380,6 +380,7 @@ 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>;