@penkov/swagger-code-gen 1.4.0 → 1.5.1

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
@@ -106,7 +106,7 @@ export interface ApiResponse {
106
106
  */
107
107
  export async function updatePet(
108
108
  body: Pet,
109
- requestOptions: RequestOptions = defaultRequestOptions()
109
+ requestOptions: RequestOptions = defReqOpts()
110
110
  ): Promise<Pet> {
111
111
  let query = '';
112
112
  const request = new Request(`${requestOptions.apiPrefix}/pet${query}`, {
@@ -133,7 +133,7 @@ export async function updatePet(
133
133
  */
134
134
  export async function findPetsByStatus(
135
135
  status: 'available' | 'pending' | 'sold' = 'available',
136
- requestOptions: RequestOptions = defaultRequestOptions()
136
+ requestOptions: RequestOptions = defReqOpts()
137
137
  ): Promise<ReadonlyArray<Pet>> {
138
138
  let query = '';
139
139
  const queryParams = [];
@@ -148,19 +148,7 @@ export async function findPetsByStatus(
148
148
  ...option(requestOptions.headers).getOrElseValue({}),
149
149
  }
150
150
  });
151
- const preProcessed = option(requestOptions.preProcessRequest).map(cb => cb(request)).getOrElseValue(request);
152
- const resp = await fetch(preProcessed);
153
- if (resp.ok) {
154
- const postProcessed = option(requestOptions.postProcessResponse)
155
- .map(cb => cb(preProcessed, resp))
156
- .getOrElseValue(resp);
157
- const json = option(resp.headers.get('content-length'))
158
- .map(x => parseInt(x))
159
- .getOrElseValue(0) > 0 ? await postProcessed.json() : null;
160
- return json as ReadonlyArray<Pet>;
161
- } else {
162
- throw resp;
163
- }
151
+ return requestImpl<Pet>(request, requestOptions);
164
152
  }
165
153
 
166
154
 
@@ -170,48 +158,53 @@ export async function findPetsByStatus(
170
158
 
171
159
  // scats wrappers
172
160
 
173
- export class OrderDto {
161
+
162
+ export class PetDto {
174
163
 
175
164
  constructor(
176
165
  readonly id: number,
177
- readonly petId: number,
178
- readonly quantity: number,
179
- readonly shipDate: string,
166
+ readonly name: string,
167
+ readonly category: CategoryDto,
168
+ readonly photoUrls: Collection<string>,
169
+ readonly tags: Collection<TagDto>,
180
170
  readonly status: string,
181
- readonly complete: boolean,
182
171
  ) {}
183
172
 
184
173
 
185
- static fromJson(json: Order): OrderDto {
186
- return new OrderDto(
174
+ static fromJson(json: Pet): PetDto {
175
+ return new PetDto(
187
176
  json.id,
188
- json.petId,
189
- json.quantity,
190
- json.shipDate,
177
+ json.name,
178
+ CategoryDto.fromJson(json.category),
179
+ Collection.from(option(json.photoUrls).getOrElseValue([]))
180
+ ,
181
+ Collection.from(option(json.tags).getOrElseValue([]))
182
+ .map(i => TagDto.fromJson(i)),
191
183
  json.status,
192
- json.complete,
193
184
  );
194
185
  }
195
186
 
196
- copy(fields: Partial<OrderDto>): OrderDto {
197
- return new OrderDto(
187
+ copy(fields: Partial<PetDto>): PetDto {
188
+ return new PetDto(
198
189
  option(fields.id).getOrElseValue(this.id),
199
- option(fields.petId).getOrElseValue(this.petId),
200
- option(fields.quantity).getOrElseValue(this.quantity),
201
- option(fields.shipDate).getOrElseValue(this.shipDate),
190
+ option(fields.name).getOrElseValue(this.name),
191
+ option(fields.category).getOrElseValue(this.category),
192
+ option(fields.photoUrls).getOrElseValue(this.photoUrls),
193
+ option(fields.tags).getOrElseValue(this.tags),
202
194
  option(fields.status).getOrElseValue(this.status),
203
- option(fields.complete).getOrElseValue(this.complete),
204
195
  );
205
196
  }
206
197
 
207
- get toJson(): Order {
198
+ get toJson(): Pet {
208
199
  return {
209
200
  id: this.id,
210
- petId: this.petId,
211
- quantity: this.quantity,
212
- shipDate: this.shipDate,
201
+ name: this.name,
202
+ category: this.category.toJson,
203
+ photoUrls: this.photoUrls
204
+ .toArray,
205
+ tags: this.tags
206
+ .map(_ => _.toJson) .toArray,
213
207
  status: this.status,
214
- complete: this.complete,
215
208
  };
216
209
  }
217
210
  }
@@ -219,17 +212,18 @@ export class OrderDto {
219
212
 
220
213
  export class ApiClient {
221
214
 
222
- constructor(private readonly requestOptions: RequestOptions = defaultRequestOptions()) {
215
+ constructor(private readonly requestOptions: RequestOptions = defReqOpts()) {
223
216
  }
224
217
 
218
+ // ... some methods skipped
225
219
 
226
220
  async updatePet(
227
- body: Pet,
221
+ body: PetDto,
228
222
  requestOptions: RequestOptions = this.requestOptions
229
223
  ): Promise<TryLike<PetDto>> {
230
224
  return (await Try.promise(() =>
231
225
  updatePet(
232
- body,
226
+ body.toJson,
233
227
  requestOptions
234
228
  )
235
229
  ))
@@ -237,17 +231,18 @@ export class ApiClient {
237
231
  ;
238
232
  }
239
233
 
240
- async addPet(
241
- body: Pet,
234
+ async findPetsByStatus(
235
+ status: 'available' | 'pending' | 'sold',
242
236
  requestOptions: RequestOptions = this.requestOptions
243
- ): Promise<TryLike<PetDto>> {
237
+ ): Promise<TryLike<Collection<PetDto>>> {
244
238
  return (await Try.promise(() =>
245
- addPet(
246
- body,
239
+ findPetsByStatus(
240
+ status,
247
241
  requestOptions
248
242
  )
249
243
  ))
250
- .map(res => PetDto.fromJson(res))
244
+ .map(res => Collection.from(option(res).getOrElseValue([])))
245
+ .map(items => items.map(i => PetDto.fromJson(i)))
251
246
  ;
252
247
  }
253
248
 
package/dist/method.js CHANGED
@@ -41,7 +41,19 @@ export class Method {
41
41
  return mimeTypes
42
42
  .find(_ => _ === 'application/json')
43
43
  .orElseValue(mimeTypes.headOption)
44
- .map(mt => SchemaFactory.build('body', body[mt].schema, schemasTypes, options));
44
+ .map(mt => {
45
+ const bodySchemaDef = body[mt].schema;
46
+ const res = SchemaFactory.build('body', bodySchemaDef, schemasTypes, options);
47
+ if (res.schemaType === 'property') {
48
+ const bProperty = res;
49
+ return bProperty.copy({
50
+ nullable: bProperty.referencesObject ? option(bodySchemaDef['nullable']).contains(true) : bProperty.nullable
51
+ });
52
+ }
53
+ else {
54
+ return res;
55
+ }
56
+ });
45
57
  });
46
58
  this.bodyDescription = option(def.requestBody).flatMap(body => option(body.description));
47
59
  const statusCodes = Collection.from(Object.keys(def.responses))
@@ -23,7 +23,7 @@ export interface RequestOptions {
23
23
  }
24
24
 
25
25
 
26
- export const defaultReqOpts: RequestOptions = {
26
+ export const defaultRequestOptions: RequestOptions = {
27
27
  apiPrefix: '',
28
28
  headers: {
29
29
  'Accept': 'application/json',
@@ -31,7 +31,7 @@ export const defaultReqOpts: RequestOptions = {
31
31
  }
32
32
  };
33
33
 
34
- const defaultRequestOptions = () => defaultReqOpts;
34
+ const defReqOpts = () => defaultRequestOptions;
35
35
 
36
36
 
37
37
  async function requestImpl<T>(request: Request, requestOptions: RequestOptions): Promise<T> {
@@ -33,7 +33,7 @@ export async function <%= method.endpointName %>(
33
33
  <%_ if (method.body.nonEmpty) { -%>
34
34
  body: <%- method.body.get.jsType %>,
35
35
  <%_ } -%>
36
- requestOptions: RequestOptions = defaultRequestOptions()
36
+ requestOptions: RequestOptions = defReqOpts()
37
37
  ): Promise<<%- method.response.responseType %>> {
38
38
  let query = '';
39
39
  <%_ if (method.parameters.filter(x => x.in === 'query').nonEmpty) { -%>
@@ -1,6 +1,6 @@
1
1
  export class ApiClient {
2
2
 
3
- constructor(private readonly requestOptions: RequestOptions = defaultRequestOptions()) {
3
+ constructor(private readonly requestOptions: RequestOptions = defReqOpts()) {
4
4
  }
5
5
 
6
6
 
@@ -18,7 +18,7 @@ export class ApiClient {
18
18
  },
19
19
  <%_ } -%>
20
20
  <%_ method.body.foreach(body => { -%>
21
- body: <%- body.jsType %>,
21
+ body: <%- body.scatsWrapperType %>,
22
22
  <%_ }); -%>
23
23
  requestOptions: RequestOptions = this.requestOptions
24
24
  ): Promise<TryLike<<%- method.response.asProperty.scatsWrapperType %>>> {
@@ -31,9 +31,23 @@ export class ApiClient {
31
31
  <%_ } else { -%>
32
32
  params,
33
33
  <%_ } -%>
34
- <%_ if (method.body.nonEmpty) { -%>
34
+ <%_ method.body.foreach(body => { -%>
35
+ <%_ if (body.schemaType === 'object') { _%>
36
+ body.toJson,
37
+ <%_ } else if (body.schemaType === 'property') { _%>
38
+ <%_ if (body.isArray && body.itemReferencesObject) { _%>
39
+ body.map(_ => _.toJson).toArray,
40
+ <%_ } else if (body.isArray && !body.itemReferencesObject) { -%>
41
+ body.toArray,
42
+ <%_ } else if (body.referencesObject) { -%>
43
+ body.toJson,
44
+ <%_ } else { -%>
45
+ body,
46
+ <%_ } -%>
47
+ <%_ } else { -%>
35
48
  body,
36
49
  <%_ } -%>
50
+ <%_ }); -%>
37
51
  requestOptions
38
52
  )
39
53
  ))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@penkov/swagger-code-gen",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "generate-client": "./dist/cli.mjs"