osury 0.5.1 → 0.6.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "osury",
3
3
  "type": "module",
4
4
  "description": "Generate ReScript types with Sury schemas from OpenAPI specifications",
5
- "version": "0.5.1",
5
+ "version": "0.6.0",
6
6
  "license": "MIT",
7
7
  "bin": {
8
8
  "osury": "./bin/osury.mjs"
@@ -4,9 +4,134 @@ import * as Errors from "./Errors.res.mjs";
4
4
  import * as Schema from "./Schema.res.mjs";
5
5
  import * as Core__Array from "@rescript/core/src/Core__Array.res.mjs";
6
6
 
7
- function parseDocument(json) {
8
- if (typeof json === "object" && json !== null && !Array.isArray(json)) {
9
- let match = json["components"];
7
+ function pathToName(path) {
8
+ return path.split("/").filter(s => {
9
+ if (s !== "") {
10
+ return !s.startsWith("{");
11
+ } else {
12
+ return false;
13
+ }
14
+ }).map(segment => segment.split("-").map(part => {
15
+ let first = part.charAt(0).toUpperCase();
16
+ let rest = part.slice(1);
17
+ return first + rest;
18
+ }).join("")).join("");
19
+ }
20
+
21
+ function ucFirst(s) {
22
+ let first = s.charAt(0).toUpperCase();
23
+ let rest = s.slice(1);
24
+ return first + rest;
25
+ }
26
+
27
+ function parsePathResponses(pathsJson) {
28
+ if (typeof pathsJson !== "object" || pathsJson === null || Array.isArray(pathsJson)) {
29
+ return {
30
+ TAG: "Ok",
31
+ _0: []
32
+ };
33
+ }
34
+ let results = Object.entries(pathsJson).flatMap(param => {
35
+ let methodsJson = param[1];
36
+ let path = param[0];
37
+ if (typeof methodsJson === "object" && methodsJson !== null && !Array.isArray(methodsJson)) {
38
+ return Core__Array.filterMap(Object.entries(methodsJson), param => {
39
+ let opJson = param[1];
40
+ let method = param[0];
41
+ let httpMethods = [
42
+ "get",
43
+ "post",
44
+ "put",
45
+ "patch",
46
+ "delete"
47
+ ];
48
+ if (!httpMethods.includes(method)) {
49
+ return;
50
+ }
51
+ if (typeof opJson !== "object" || opJson === null || Array.isArray(opJson)) {
52
+ return;
53
+ }
54
+ let match = opJson["responses"];
55
+ if (match === undefined) {
56
+ return;
57
+ }
58
+ if (typeof match !== "object" || match === null || Array.isArray(match)) {
59
+ return;
60
+ }
61
+ let r = match["200"];
62
+ let responseJson = r !== undefined ? r : match["201"];
63
+ if (responseJson === undefined) {
64
+ return;
65
+ }
66
+ if (typeof responseJson !== "object" || responseJson === null || Array.isArray(responseJson)) {
67
+ return;
68
+ }
69
+ let match$1 = responseJson["content"];
70
+ if (match$1 === undefined) {
71
+ return;
72
+ }
73
+ if (typeof match$1 !== "object" || match$1 === null || Array.isArray(match$1)) {
74
+ return;
75
+ }
76
+ let match$2 = match$1["application/json"];
77
+ if (match$2 === undefined) {
78
+ return;
79
+ }
80
+ if (typeof match$2 !== "object" || match$2 === null || Array.isArray(match$2)) {
81
+ return;
82
+ }
83
+ let schemaJson = match$2["schema"];
84
+ if (schemaJson === undefined) {
85
+ return;
86
+ }
87
+ let name = ucFirst(method) + pathToName(path) + "Response";
88
+ let schemaType = Schema.parse(schemaJson);
89
+ if (schemaType.TAG === "Ok") {
90
+ return {
91
+ TAG: "Ok",
92
+ _0: {
93
+ name: name,
94
+ schema: schemaType._0
95
+ }
96
+ };
97
+ } else {
98
+ return {
99
+ TAG: "Error",
100
+ _0: schemaType._0
101
+ };
102
+ }
103
+ });
104
+ } else {
105
+ return [];
106
+ }
107
+ });
108
+ let errors = Core__Array.filterMap(results, r => {
109
+ if (r.TAG === "Ok") {
110
+ return;
111
+ } else {
112
+ return r._0;
113
+ }
114
+ }).flat();
115
+ if (errors.length > 0) {
116
+ return {
117
+ TAG: "Error",
118
+ _0: errors
119
+ };
120
+ }
121
+ let schemas = Core__Array.filterMap(results, r => {
122
+ if (r.TAG === "Ok") {
123
+ return r._0;
124
+ }
125
+ });
126
+ return {
127
+ TAG: "Ok",
128
+ _0: schemas
129
+ };
130
+ }
131
+
132
+ function parseComponentSchemas(componentsJson) {
133
+ if (typeof componentsJson === "object" && componentsJson !== null && !Array.isArray(componentsJson)) {
134
+ let match = componentsJson["schemas"];
10
135
  if (match === undefined) {
11
136
  return {
12
137
  TAG: "Ok",
@@ -15,79 +140,105 @@ function parseDocument(json) {
15
140
  }
16
141
  let exit = 0;
17
142
  if (typeof match === "object" && match !== null && !Array.isArray(match)) {
18
- let match$1 = match["schemas"];
19
- if (match$1 === undefined) {
20
- return {
21
- TAG: "Ok",
22
- _0: []
23
- };
24
- }
25
- let exit$1 = 0;
26
- if (typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1)) {
27
- let entries = Object.entries(match$1);
28
- let results = entries.map(param => {
29
- let schemaType = Schema.parse(param[1]);
30
- if (schemaType.TAG === "Ok") {
31
- return {
32
- TAG: "Ok",
33
- _0: {
34
- name: param[0],
35
- schema: schemaType._0
36
- }
37
- };
38
- } else {
39
- return {
40
- TAG: "Error",
41
- _0: schemaType._0
42
- };
43
- }
44
- });
45
- let errors = Core__Array.filterMap(results, r => {
46
- if (r.TAG === "Ok") {
47
- return;
48
- } else {
49
- return r._0;
50
- }
51
- }).flat();
52
- if (errors.length > 0) {
143
+ let entries = Object.entries(match);
144
+ let results = entries.map(param => {
145
+ let schemaType = Schema.parse(param[1]);
146
+ if (schemaType.TAG === "Ok") {
147
+ return {
148
+ TAG: "Ok",
149
+ _0: {
150
+ name: param[0],
151
+ schema: schemaType._0
152
+ }
153
+ };
154
+ } else {
53
155
  return {
54
156
  TAG: "Error",
55
- _0: errors
157
+ _0: schemaType._0
56
158
  };
57
159
  }
58
- let schemas = Core__Array.filterMap(results, r => {
59
- if (r.TAG === "Ok") {
60
- return r._0;
61
- }
62
- });
63
- return {
64
- TAG: "Ok",
65
- _0: schemas
66
- };
67
- }
68
- exit$1 = 3;
69
- if (exit$1 === 3) {
160
+ });
161
+ let errors = Core__Array.filterMap(results, r => {
162
+ if (r.TAG === "Ok") {
163
+ return;
164
+ } else {
165
+ return r._0;
166
+ }
167
+ }).flat();
168
+ if (errors.length > 0) {
70
169
  return {
71
170
  TAG: "Error",
72
- _0: [Errors.makeError({
73
- TAG: "InvalidJson",
74
- _0: "schemas must be an object"
75
- }, undefined, undefined, undefined)]
171
+ _0: errors
76
172
  };
77
173
  }
78
- } else {
79
- exit = 2;
174
+ let schemas = Core__Array.filterMap(results, r => {
175
+ if (r.TAG === "Ok") {
176
+ return r._0;
177
+ }
178
+ });
179
+ return {
180
+ TAG: "Ok",
181
+ _0: schemas
182
+ };
80
183
  }
184
+ exit = 2;
81
185
  if (exit === 2) {
82
186
  return {
83
187
  TAG: "Error",
84
188
  _0: [Errors.makeError({
85
189
  TAG: "InvalidJson",
86
- _0: "components must be an object"
190
+ _0: "schemas must be an object"
87
191
  }, undefined, undefined, undefined)]
88
192
  };
89
193
  }
90
194
  }
195
+ return {
196
+ TAG: "Error",
197
+ _0: [Errors.makeError({
198
+ TAG: "InvalidJson",
199
+ _0: "components must be an object"
200
+ }, undefined, undefined, undefined)]
201
+ };
202
+ }
203
+
204
+ function parseDocument(json) {
205
+ if (typeof json === "object" && json !== null && !Array.isArray(json)) {
206
+ let componentsJson = json["components"];
207
+ let componentSchemas = componentsJson !== undefined ? parseComponentSchemas(componentsJson) : ({
208
+ TAG: "Ok",
209
+ _0: []
210
+ });
211
+ let pathsJson = json["paths"];
212
+ let pathSchemas = pathsJson !== undefined ? parsePathResponses(pathsJson) : ({
213
+ TAG: "Ok",
214
+ _0: []
215
+ });
216
+ if (componentSchemas.TAG === "Ok") {
217
+ if (pathSchemas.TAG === "Ok") {
218
+ return {
219
+ TAG: "Ok",
220
+ _0: componentSchemas._0.concat(pathSchemas._0)
221
+ };
222
+ } else {
223
+ return {
224
+ TAG: "Error",
225
+ _0: pathSchemas._0
226
+ };
227
+ }
228
+ }
229
+ let e = componentSchemas._0;
230
+ if (pathSchemas.TAG === "Ok") {
231
+ return {
232
+ TAG: "Error",
233
+ _0: e
234
+ };
235
+ } else {
236
+ return {
237
+ TAG: "Error",
238
+ _0: e.concat(pathSchemas._0)
239
+ };
240
+ }
241
+ }
91
242
  return {
92
243
  TAG: "Error",
93
244
  _0: [Errors.makeError({
@@ -98,6 +249,10 @@ function parseDocument(json) {
98
249
  }
99
250
 
100
251
  export {
252
+ pathToName,
253
+ ucFirst,
254
+ parsePathResponses,
255
+ parseComponentSchemas,
101
256
  parseDocument,
102
257
  }
103
258
  /* No side effect */