@effect/openapi-generator 4.0.0-beta.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/LICENSE +21 -0
- package/dist/JsonSchemaGenerator.d.ts +8 -0
- package/dist/JsonSchemaGenerator.d.ts.map +1 -0
- package/dist/JsonSchemaGenerator.js +75 -0
- package/dist/JsonSchemaGenerator.js.map +1 -0
- package/dist/OpenApiGenerator.d.ts +32 -0
- package/dist/OpenApiGenerator.d.ts.map +1 -0
- package/dist/OpenApiGenerator.js +206 -0
- package/dist/OpenApiGenerator.js.map +1 -0
- package/dist/OpenApiPatch.d.ts +296 -0
- package/dist/OpenApiPatch.d.ts.map +1 -0
- package/dist/OpenApiPatch.js +448 -0
- package/dist/OpenApiPatch.js.map +1 -0
- package/dist/OpenApiTransformer.d.ts +24 -0
- package/dist/OpenApiTransformer.d.ts.map +1 -0
- package/dist/OpenApiTransformer.js +740 -0
- package/dist/OpenApiTransformer.js.map +1 -0
- package/dist/ParsedOperation.d.ts +29 -0
- package/dist/ParsedOperation.d.ts.map +1 -0
- package/dist/ParsedOperation.js +13 -0
- package/dist/ParsedOperation.js.map +1 -0
- package/dist/Utils.d.ts +6 -0
- package/dist/Utils.d.ts.map +1 -0
- package/dist/Utils.js +42 -0
- package/dist/Utils.js.map +1 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +7 -0
- package/dist/bin.js.map +1 -0
- package/dist/main.d.ts +5 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +47 -0
- package/dist/main.js.map +1 -0
- package/package.json +67 -0
- package/src/JsonSchemaGenerator.ts +94 -0
- package/src/OpenApiGenerator.ts +309 -0
- package/src/OpenApiPatch.ts +514 -0
- package/src/OpenApiTransformer.ts +954 -0
- package/src/ParsedOperation.ts +43 -0
- package/src/Utils.ts +50 -0
- package/src/bin.ts +10 -0
- package/src/main.ts +68 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAPI spec patching utilities.
|
|
3
|
+
*
|
|
4
|
+
* Handles parsing and applying JSON Patch documents (RFC 6902) to OpenAPI
|
|
5
|
+
* specs. Supports patches from:
|
|
6
|
+
* - JSON files (.json)
|
|
7
|
+
* - YAML files (.yaml, .yml)
|
|
8
|
+
* - Inline JSON strings
|
|
9
|
+
*
|
|
10
|
+
* @module OpenApiPatch
|
|
11
|
+
*/
|
|
12
|
+
import * as Effect from "effect/Effect";
|
|
13
|
+
import * as FileSystem from "effect/FileSystem";
|
|
14
|
+
import * as JsonPatch from "effect/JsonPatch";
|
|
15
|
+
import * as Path from "effect/Path";
|
|
16
|
+
import * as Schema from "effect/Schema";
|
|
17
|
+
declare const JsonPatchParseError_base: Schema.ErrorClass<unknown, Schema.Struct<{
|
|
18
|
+
readonly _tag: Schema.tag<"JsonPatchParseError">;
|
|
19
|
+
readonly source: Schema.String;
|
|
20
|
+
readonly reason: Schema.String;
|
|
21
|
+
}>, import("effect/Cause").YieldableError>;
|
|
22
|
+
/**
|
|
23
|
+
* Error thrown when parsing a JSON Patch input fails.
|
|
24
|
+
*
|
|
25
|
+
* This error occurs when:
|
|
26
|
+
* - A patch file cannot be read
|
|
27
|
+
* - JSON or YAML syntax is invalid
|
|
28
|
+
* - The file format is unsupported
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import * as OpenApiPatch from "@effect/openapi-generator/OpenApiPatch"
|
|
33
|
+
*
|
|
34
|
+
* const error = new OpenApiPatch.JsonPatchParseError({
|
|
35
|
+
* source: "./patches/fix.json",
|
|
36
|
+
* reason: "Unexpected token at position 42"
|
|
37
|
+
* })
|
|
38
|
+
*
|
|
39
|
+
* console.log(error.message)
|
|
40
|
+
* // "Failed to parse patch from ./patches/fix.json: Unexpected token at position 42"
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @since 1.0.0
|
|
44
|
+
* @category errors
|
|
45
|
+
*/
|
|
46
|
+
export declare class JsonPatchParseError extends JsonPatchParseError_base {
|
|
47
|
+
get message(): string;
|
|
48
|
+
}
|
|
49
|
+
declare const JsonPatchValidationError_base: Schema.ErrorClass<unknown, Schema.Struct<{
|
|
50
|
+
readonly _tag: Schema.tag<"JsonPatchValidationError">;
|
|
51
|
+
readonly source: Schema.String;
|
|
52
|
+
readonly reason: Schema.String;
|
|
53
|
+
}>, import("effect/Cause").YieldableError>;
|
|
54
|
+
/**
|
|
55
|
+
* Error thrown when a parsed value does not conform to the JSON Patch schema.
|
|
56
|
+
*
|
|
57
|
+
* This error occurs when:
|
|
58
|
+
* - The patch is not an array
|
|
59
|
+
* - An operation is missing required fields (op, path)
|
|
60
|
+
* - An operation has an unsupported op value
|
|
61
|
+
* - An add/replace operation is missing the value field
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* import * as OpenApiPatch from "@effect/openapi-generator/OpenApiPatch"
|
|
66
|
+
*
|
|
67
|
+
* const error = new OpenApiPatch.JsonPatchValidationError({
|
|
68
|
+
* source: "inline",
|
|
69
|
+
* reason: "Expected 'add' | 'remove' | 'replace' at [0].op, got 'copy'"
|
|
70
|
+
* })
|
|
71
|
+
*
|
|
72
|
+
* console.log(error.message)
|
|
73
|
+
* // "Invalid JSON Patch from inline: Expected 'add' | 'remove' | 'replace' at [0].op, got 'copy'"
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @since 1.0.0
|
|
77
|
+
* @category errors
|
|
78
|
+
*/
|
|
79
|
+
export declare class JsonPatchValidationError extends JsonPatchValidationError_base {
|
|
80
|
+
get message(): string;
|
|
81
|
+
}
|
|
82
|
+
declare const JsonPatchApplicationError_base: Schema.ErrorClass<unknown, Schema.Struct<{
|
|
83
|
+
readonly _tag: Schema.tag<"JsonPatchApplicationError">;
|
|
84
|
+
readonly source: Schema.String;
|
|
85
|
+
readonly operationIndex: Schema.Number;
|
|
86
|
+
readonly operation: Schema.String;
|
|
87
|
+
readonly path: Schema.String;
|
|
88
|
+
readonly reason: Schema.String;
|
|
89
|
+
}>, import("effect/Cause").YieldableError>;
|
|
90
|
+
/**
|
|
91
|
+
* Error thrown when applying a JSON Patch operation fails.
|
|
92
|
+
*
|
|
93
|
+
* This error occurs when:
|
|
94
|
+
* - A path does not exist for remove/replace operations
|
|
95
|
+
* - An array index is out of bounds
|
|
96
|
+
* - The target location is not a valid container
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import * as OpenApiPatch from "@effect/openapi-generator/OpenApiPatch"
|
|
101
|
+
*
|
|
102
|
+
* const error = new OpenApiPatch.JsonPatchApplicationError({
|
|
103
|
+
* source: "./patches/fix.json",
|
|
104
|
+
* operationIndex: 2,
|
|
105
|
+
* operation: "remove",
|
|
106
|
+
* path: "/paths/~1users",
|
|
107
|
+
* reason: "Property \"users\" does not exist"
|
|
108
|
+
* })
|
|
109
|
+
*
|
|
110
|
+
* console.log(error.message)
|
|
111
|
+
* // "Failed to apply patch from ./patches/fix.json: operation 2 (remove at /paths/~1users): Property \"users\" does not exist"
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* @since 1.0.0
|
|
115
|
+
* @category errors
|
|
116
|
+
*/
|
|
117
|
+
export declare class JsonPatchApplicationError extends JsonPatchApplicationError_base {
|
|
118
|
+
get message(): string;
|
|
119
|
+
}
|
|
120
|
+
declare const JsonPatchAggregateError_base: Schema.ErrorClass<unknown, Schema.Struct<{
|
|
121
|
+
readonly _tag: Schema.tag<"JsonPatchAggregateError">;
|
|
122
|
+
readonly errors: Schema.Array$<Schema.Unknown>;
|
|
123
|
+
}>, import("effect/Cause").YieldableError>;
|
|
124
|
+
/**
|
|
125
|
+
* Error thrown when multiple JSON Patch operations fail.
|
|
126
|
+
*
|
|
127
|
+
* This error aggregates all application errors so users can see every
|
|
128
|
+
* failing operation at once instead of fixing them one at a time.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
* ```ts
|
|
132
|
+
* import * as OpenApiPatch from "@effect/openapi-generator/OpenApiPatch"
|
|
133
|
+
*
|
|
134
|
+
* const error = new OpenApiPatch.JsonPatchAggregateError({
|
|
135
|
+
* errors: [
|
|
136
|
+
* new OpenApiPatch.JsonPatchApplicationError({
|
|
137
|
+
* source: "./fix.json",
|
|
138
|
+
* operationIndex: 0,
|
|
139
|
+
* operation: "replace",
|
|
140
|
+
* path: "/info/x",
|
|
141
|
+
* reason: "Property does not exist"
|
|
142
|
+
* }),
|
|
143
|
+
* new OpenApiPatch.JsonPatchApplicationError({
|
|
144
|
+
* source: "./fix.json",
|
|
145
|
+
* operationIndex: 2,
|
|
146
|
+
* operation: "remove",
|
|
147
|
+
* path: "/paths/~1users",
|
|
148
|
+
* reason: "Property does not exist"
|
|
149
|
+
* })
|
|
150
|
+
* ]
|
|
151
|
+
* })
|
|
152
|
+
*
|
|
153
|
+
* console.log(error.message)
|
|
154
|
+
* // "2 patch operations failed:\n 1. ..."
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* @since 1.0.0
|
|
158
|
+
* @category errors
|
|
159
|
+
*/
|
|
160
|
+
export declare class JsonPatchAggregateError extends JsonPatchAggregateError_base {
|
|
161
|
+
get message(): string;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Schema for a JSON Patch "add" operation.
|
|
165
|
+
*
|
|
166
|
+
* @since 1.0.0
|
|
167
|
+
* @category schemas
|
|
168
|
+
*/
|
|
169
|
+
export declare const JsonPatchAdd: Schema.Codec<Extract<JsonPatch.JsonPatchOperation, {
|
|
170
|
+
op: "add";
|
|
171
|
+
}>>;
|
|
172
|
+
/**
|
|
173
|
+
* Schema for a JSON Patch "remove" operation.
|
|
174
|
+
*
|
|
175
|
+
* @since 1.0.0
|
|
176
|
+
* @category schemas
|
|
177
|
+
*/
|
|
178
|
+
export declare const JsonPatchRemove: Schema.Codec<Extract<JsonPatch.JsonPatchOperation, {
|
|
179
|
+
op: "remove";
|
|
180
|
+
}>>;
|
|
181
|
+
/**
|
|
182
|
+
* Schema for a JSON Patch "replace" operation.
|
|
183
|
+
*
|
|
184
|
+
* @since 1.0.0
|
|
185
|
+
* @category schemas
|
|
186
|
+
*/
|
|
187
|
+
export declare const JsonPatchReplace: Schema.Codec<Extract<JsonPatch.JsonPatchOperation, {
|
|
188
|
+
op: "replace";
|
|
189
|
+
}>>;
|
|
190
|
+
/**
|
|
191
|
+
* Schema for a single JSON Patch operation.
|
|
192
|
+
*
|
|
193
|
+
* Supports the subset of RFC 6902 operations that Effect's JsonPatch module
|
|
194
|
+
* implements: `add`, `remove`, and `replace`.
|
|
195
|
+
*
|
|
196
|
+
* @since 1.0.0
|
|
197
|
+
* @category schemas
|
|
198
|
+
*/
|
|
199
|
+
export declare const JsonPatchOperation: Schema.Codec<JsonPatch.JsonPatchOperation>;
|
|
200
|
+
/**
|
|
201
|
+
* Schema for a JSON Patch document (array of operations).
|
|
202
|
+
*
|
|
203
|
+
* A JSON Patch document is an ordered list of operations to apply to a JSON
|
|
204
|
+
* document. Operations are applied in sequence, with each operation seeing
|
|
205
|
+
* the result of previous operations.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* import { Schema } from "effect"
|
|
210
|
+
* import * as OpenApiPatch from "@effect/openapi-generator/OpenApiPatch"
|
|
211
|
+
*
|
|
212
|
+
* const patch = Schema.decodeUnknownSync(OpenApiPatch.JsonPatchDocument)([
|
|
213
|
+
* { op: "add", path: "/foo", value: "bar" },
|
|
214
|
+
* { op: "remove", path: "/baz" },
|
|
215
|
+
* { op: "replace", path: "/qux", value: 42 }
|
|
216
|
+
* ])
|
|
217
|
+
* ```
|
|
218
|
+
*
|
|
219
|
+
* @since 1.0.0
|
|
220
|
+
* @category schemas
|
|
221
|
+
*/
|
|
222
|
+
export declare const JsonPatchDocument: Schema.Array$<Schema.Codec<JsonPatch.JsonPatchOperation, JsonPatch.JsonPatchOperation, never, never>>;
|
|
223
|
+
/**
|
|
224
|
+
* Type for a JSON Patch document.
|
|
225
|
+
*
|
|
226
|
+
* @since 1.0.0
|
|
227
|
+
* @category types
|
|
228
|
+
*/
|
|
229
|
+
export type JsonPatchDocument = typeof JsonPatchDocument.Type;
|
|
230
|
+
/**
|
|
231
|
+
* Parse a JSON Patch from either a file path or inline JSON string.
|
|
232
|
+
*
|
|
233
|
+
* The input is first checked as a file path. If the file exists, it is read
|
|
234
|
+
* and parsed based on its extension (.json, .yaml, .yml). Otherwise, the
|
|
235
|
+
* input is parsed as inline JSON.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* import { Effect } from "effect"
|
|
240
|
+
* import * as OpenApiPatch from "@effect/openapi-generator/OpenApiPatch"
|
|
241
|
+
*
|
|
242
|
+
* // From inline JSON
|
|
243
|
+
* const fromInline = OpenApiPatch.parsePatchInput(
|
|
244
|
+
* '[{"op":"replace","path":"/info/title","value":"My API"}]'
|
|
245
|
+
* )
|
|
246
|
+
*
|
|
247
|
+
* // From file path
|
|
248
|
+
* const fromFile = OpenApiPatch.parsePatchInput("./patches/fix-api.json")
|
|
249
|
+
*
|
|
250
|
+
* const program = Effect.gen(function*() {
|
|
251
|
+
* const patch = yield* fromInline
|
|
252
|
+
* console.log(patch)
|
|
253
|
+
* // [{ op: "replace", path: "/info/title", value: "My API" }]
|
|
254
|
+
* })
|
|
255
|
+
* ```
|
|
256
|
+
*
|
|
257
|
+
* @since 1.0.0
|
|
258
|
+
* @category parsing
|
|
259
|
+
*/
|
|
260
|
+
export declare const parsePatchInput: (input: string) => Effect.Effect<readonly JsonPatch.JsonPatchOperation[], JsonPatchParseError | JsonPatchValidationError, Path.Path | FileSystem.FileSystem>;
|
|
261
|
+
/**
|
|
262
|
+
* Apply a sequence of JSON patches to a document.
|
|
263
|
+
*
|
|
264
|
+
* Patches are applied in order, with each patch operating on the result of
|
|
265
|
+
* the previous one. All operations are attempted, and if any fail, the errors
|
|
266
|
+
* are accumulated and reported together so users can fix all issues at once.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { Effect } from "effect"
|
|
271
|
+
* import * as OpenApiPatch from "@effect/openapi-generator/OpenApiPatch"
|
|
272
|
+
*
|
|
273
|
+
* const document = { info: { title: "Old Title" }, paths: {} }
|
|
274
|
+
* const patches = [
|
|
275
|
+
* {
|
|
276
|
+
* source: "inline",
|
|
277
|
+
* patch: [{ op: "replace" as const, path: "/info/title", value: "New Title" }]
|
|
278
|
+
* }
|
|
279
|
+
* ]
|
|
280
|
+
*
|
|
281
|
+
* const program = Effect.gen(function*() {
|
|
282
|
+
* const result = yield* OpenApiPatch.applyPatches(patches, document)
|
|
283
|
+
* console.log(result)
|
|
284
|
+
* // { info: { title: "New Title" }, paths: {} }
|
|
285
|
+
* })
|
|
286
|
+
* ```
|
|
287
|
+
*
|
|
288
|
+
* @since 1.0.0
|
|
289
|
+
* @category application
|
|
290
|
+
*/
|
|
291
|
+
export declare const applyPatches: (patches: readonly {
|
|
292
|
+
readonly source: string;
|
|
293
|
+
readonly patch: JsonPatchDocument;
|
|
294
|
+
}[], document: Schema.Json) => Effect.Effect<Schema.Json, JsonPatchAggregateError, never>;
|
|
295
|
+
export {};
|
|
296
|
+
//# sourceMappingURL=OpenApiPatch.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenApiPatch.d.ts","sourceRoot":"","sources":["../src/OpenApiPatch.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;AACvC,OAAO,KAAK,UAAU,MAAM,mBAAmB,CAAA;AAE/C,OAAO,KAAK,SAAS,MAAM,kBAAkB,CAAA;AAC7C,OAAO,KAAK,IAAI,MAAM,aAAa,CAAA;AAEnC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAA;;;;;;AAOvC;;;;;;;;;;;;;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