@weclapp/sdk 2.0.0-dev.41 → 2.0.0-dev.43

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.
Files changed (3) hide show
  1. package/README.md +57 -72
  2. package/dist/cli.js +22 -6
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -1,12 +1,6 @@
1
- <br/>
1
+ # weclapp SDK Generator
2
2
 
3
- <div align="center">
4
- <h1>weclapp sdk generator</h1>
5
- </div>
6
-
7
- <br/>
8
-
9
- # Introduction
3
+ ## Introduction
10
4
 
11
5
  This is an SDK generator, it will take an [`openapi.json`](https://swagger.io/specification/) from [weclapp](https://weclapp.com/) and build services and typescript types according to the entities defined in it. Each service is responsible for a _single_ entity. Every service contains all functions available for this
12
6
  entity, this may include functions such as `update`, `remove`, `some` and many more. With these functions you can send requests to your weclapp system to get data or manipulate them.
@@ -17,7 +11,7 @@ Check out [generated types and utilities](#generated-types-and-utilities) for mo
17
11
 
18
12
  What's being generated depends on the weclapp version you're using.
19
13
 
20
- # Getting started
14
+ ## Getting started
21
15
 
22
16
  The SDK generator requires the current or LTS version of nodejs, as well as npm v10.
23
17
 
@@ -46,7 +40,7 @@ This way, every time someone installs or updates dependencies, the SDK is genera
46
40
  }
47
41
  ```
48
42
 
49
- ## Available flags
43
+ ### Available flags
50
44
 
51
45
  | Flag | Description | Value / Type |
52
46
  | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------- |
@@ -64,12 +58,12 @@ This way, every time someone installs or updates dependencies, the SDK is genera
64
58
  After that, you can import the sdk via `@weclapp/sdk`.
65
59
  Check out the [docs](docs) for how the generated SDK looks like and how to use it!
66
60
 
67
- # Initialization
61
+ ## Initialization
68
62
 
69
63
  To initialize a service a `ServiceConfig` is needed, you can specify a global config in case you don't want to pass the config every time to the service you
70
64
  want to use manually. If you pass an empty object as ServiceConfig the [location](https://developer.mozilla.org/en-US/docs/Web/API/Window/location) will be used to get the domain and the protocol (secure config option).
71
65
 
72
- ## The Service Configuration
66
+ ### Service config
73
67
 
74
68
  The configuration looks like the following (taken from [globalConfig.ts](/src/generator/01-base/static/globalConfig.ts.txt)):
75
69
 
@@ -111,11 +105,11 @@ interface ServiceConfig {
111
105
  }
112
106
  ```
113
107
 
114
- ### Usage
108
+ ### Using the config
115
109
 
116
110
  There are three ways of accessing a service through the SDK:
117
111
 
118
- #### Using a Global Config
112
+ #### Using the global config
119
113
 
120
114
  ```ts
121
115
  import { setGlobalConfig, partyService } from '@weclapp/sdk';
@@ -130,7 +124,7 @@ const party = partyService();
130
124
  console.log(`Total amount of parties: ${await party.count()}`);
131
125
  ```
132
126
 
133
- #### Using a Config per Service
127
+ #### Using a config per service
134
128
 
135
129
  ```ts
136
130
  import { partyService } from '@weclapp/sdk';
@@ -175,53 +169,20 @@ interface RequestPayload {
175
169
  forceBlob?: boolean;
176
170
  }
177
171
  ```
172
+
178
173
  and `RequestOptions` looks like this:
174
+
179
175
  ```ts
180
176
  interface RequestOptions {
181
- signal?: AbortSignal;
177
+ signal?: AbortSignal;
182
178
  }
183
- ````
184
-
185
- #### Aborting a request
186
- To abort a request an AbortController has to be instantiated and its signal has to be passed to the request. The controller can
187
- abort the request when needed and the case can be handled with a catch.
188
- ```ts
189
- import { wServices } from "@sdk/dist";
190
-
191
- const controller = new AbortController();
192
- let count = 0;
193
-
194
- wServices.article
195
- .count(
196
- {
197
- where: {
198
- active: { EQ: true },
199
- },
200
- },
201
- { signal: controller.signal }
202
- )
203
- .then((c) => (count = c))
204
- .catch((err) => {
205
- if (controller.signal.aborted) {
206
- if (controller.signal.reason) {
207
- console.log(`Request aborted with reason: ${controller.signal.reason}`);
208
- } else {
209
- console.log('Request aborted but no reason was given.');
210
- }
211
- } else {
212
- console.log(err);
213
- }
214
- });
215
-
216
- controller.abort('Abort article count request');
217
179
  ```
218
180
 
219
-
220
- # Generated types and utilities
181
+ ## Generated types and utilities
221
182
 
222
183
  The generator generates various utilities that can be used to integrate it in a generic way into your app.
223
184
 
224
- ## Exported constants
185
+ ### Exported constants
225
186
 
226
187
  | Constant | Description |
227
188
  | --------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
@@ -232,7 +193,7 @@ The generator generates various utilities that can be used to integrate it in a
232
193
  | `wServices` | Object with all services where the name is the key and the value the service (that is using the global config). |
233
194
  | `wEntityProperties` | Object with all entity names as key and properties including the type and format as value. |
234
195
 
235
- ## Exported types
196
+ ### Exported types
236
197
 
237
198
  | Type | Description | Type guards available? |
238
199
  | ----------------------- | ----------------------------------------------------------------------------------------------------------- | ------------------------------------- |
@@ -247,7 +208,7 @@ The generator generates various utilities that can be used to integrate it in a
247
208
  | `WServices` | Type of `wServices`. | |
248
209
  | `WEntityProperties` | Generalized type of `wEntityProperties`. | |
249
210
 
250
- # Services and Raw in depth
211
+ ## Services and raw-function in depth
251
212
 
252
213
  As already described above, the api endpoints can be requested via services or the raw function. The advantage of wServices over the raw function is that all endpoints of the entities are available as functions and these functions are typed. This makes it easier to work with the data provided via the weclapp API.
253
214
 
@@ -259,7 +220,7 @@ A service of an entity has in general the following base function:
259
220
  remove: // deletes a entity
260
221
  update: // updates a entity
261
222
 
262
- In addition there are some custom endpoint functions. The generated PartyService is shown below as an example:
223
+ In addition, there are some custom endpoint functions. The generated PartyService is shown below as an example:
263
224
 
264
225
  ```ts
265
226
  interface PartyService {
@@ -276,7 +237,7 @@ interface PartyService {
276
237
  }
277
238
  ```
278
239
 
279
- ## Comparison
240
+ ### Example
280
241
 
281
242
  ```ts
282
243
  import { PartyType, setGlobalConfig, wServices } from '@weclapp/sdk';
@@ -322,9 +283,9 @@ if (contactRaw && typeof contactRaw.id === 'string') {
322
283
  }
323
284
  ```
324
285
 
325
- ## Service request arguments
286
+ ### Service request params
326
287
 
327
- ### Filtering
288
+ #### Filtering
328
289
 
329
290
  With the some and count functions you can filter the requested data.
330
291
 
@@ -377,9 +338,9 @@ wServices['article'].some({
377
338
  This is evaluated to:
378
339
  (name EQ 'toy 1' OR articleNumber EQ '12345') AND batchNumberRequired EQ true
379
340
 
380
- ### Where filter
341
+ #### Where filter
381
342
 
382
- <strong>Warning: This is still a beta feature.</strong>
343
+ > Warning: This is still a beta feature
383
344
 
384
345
  It is also possible to specify complex filter expressions that can combine multiple conditions and express relations between properties:
385
346
 
@@ -398,7 +359,7 @@ wServices['article'].some({
398
359
 
399
360
  "where" parameters are ANDed with other filter parameters.
400
361
 
401
- ### Sort
362
+ #### Sort
402
363
 
403
364
  You can sort your requested data with an array properties.
404
365
 
@@ -410,7 +371,7 @@ wServices['article'].some({
410
371
 
411
372
  Sort by name (ascending) and then minimumPurchaseQuantity descending.
412
373
 
413
- ### Pagination
374
+ #### Pagination
414
375
 
415
376
  By default the API returns only the first 100 entities. You can increase the size of one response to the maximum of 1000. To get the next 1000 entities you have increase the page number.
416
377
 
@@ -425,7 +386,7 @@ wServices['article'].some({
425
386
 
426
387
  This returns the first 10 articles of the second page.
427
388
 
428
- ### Select
389
+ #### Select
429
390
 
430
391
  With the select option you can fetch specific subset of properties:
431
392
 
@@ -437,18 +398,42 @@ wServices['article'].some({
437
398
 
438
399
  This only returns the articleNumber property of all articles.
439
400
 
440
- # Enums
401
+ ### Aborting a request
441
402
 
442
- The generated enums are a good posibility to check if an entity is of a specific type. For example, you can get all articles of a certain article type:
403
+ To abort a request an AbortController has to be instantiated and its signal has to be passed to the request. The controller can
404
+ abort the request when needed and the case can be handled with a catch.
443
405
 
444
406
  ```ts
445
- wServices['article'].some({
446
- filter: {
447
- articleType: { EQ: ArticleType.STORABLE }
448
- }
449
- });
407
+ import { wServices } from '@sdk/dist';
408
+
409
+ const controller = new AbortController();
410
+ let count = 0;
411
+
412
+ wServices.article
413
+ .count(
414
+ {
415
+ where: {
416
+ active: { EQ: true }
417
+ }
418
+ },
419
+ { signal: controller.signal }
420
+ )
421
+ .then((c) => (count = c))
422
+ .catch((err) => {
423
+ if (controller.signal.aborted) {
424
+ if (controller.signal.reason) {
425
+ console.log(`Request aborted with reason: ${controller.signal.reason}`);
426
+ } else {
427
+ console.log('Request aborted but no reason was given.');
428
+ }
429
+ } else {
430
+ console.log(err);
431
+ }
432
+ });
433
+
434
+ controller.abort('Abort article count request');
450
435
  ```
451
436
 
452
- # Contributing
437
+ ## Contributing
453
438
 
454
439
  Check out the [contributing guidelines](.github/CONTRIBUTING.md).
package/dist/cli.js CHANGED
@@ -458,7 +458,13 @@ const generateEntityFilterProps = (entities, enums) => {
458
458
  const entityFilterProps = new Map();
459
459
  const transformFilterProps = (props) => {
460
460
  return props.map((prop) => {
461
- if (!prop.type || enums.has(prop.type) || prop.type === 'string' || prop.type === 'number' || prop.type === 'boolean' || prop.type.endsWith('[]')) {
461
+ if (!prop.type ||
462
+ enums.has(prop.type) ||
463
+ prop.type === 'string' ||
464
+ prop.type === 'number' ||
465
+ prop.type === 'boolean' ||
466
+ prop.type.endsWith('[]') ||
467
+ prop.type === '{}') {
462
468
  return prop;
463
469
  }
464
470
  return { ...prop, type: `${pascalCase(prop.type)}_${FILTER_PROPS_SUFFIX}` };
@@ -636,7 +642,8 @@ const generateCountEndpoint = ({ aliases, path, target, endpoint }) => {
636
642
  const functionTypeSource = generateArrowFunctionType({
637
643
  type: functionTypeName,
638
644
  params: [
639
- `query${parametersType.isFullyOptional() ? '?' : ''}: CountQuery<${filterTypeName}>${path.parameters?.length ? ' & ' + parametersTypeName : ''}`, 'requestOptions?: RequestOptions'
645
+ `query${parametersType.isFullyOptional() ? '?' : ''}: CountQuery<${filterTypeName}>${path.parameters?.length ? ' & ' + parametersTypeName : ''}`,
646
+ 'requestOptions?: RequestOptions'
640
647
  ],
641
648
  returns: `${resolveResponseType(target)}<number>`
642
649
  });
@@ -723,7 +730,11 @@ const generateGenericEndpoint = (suffix) => ({ target, method, path, endpoint })
723
730
  const responseBody = generateResponseBodyType(path);
724
731
  const functionTypeSource = generateArrowFunctionType({
725
732
  type: functionTypeName,
726
- params: [...(hasId ? ['id: string'] : []), `query${params.isFullyOptional() ? '?' : ''}: ${entityQuery}`, 'requestOptions?: RequestOptions'],
733
+ params: [
734
+ ...(hasId ? ['id: string'] : []),
735
+ `query${params.isFullyOptional() ? '?' : ''}: ${entityQuery}`,
736
+ 'requestOptions?: RequestOptions'
737
+ ],
727
738
  returns: `${resolveResponseType(target)}<${wrapBody(responseBody, target).toString()}>`
728
739
  });
729
740
  const functionSource = generateArrowFunction({
@@ -907,7 +918,12 @@ const generateUpdateEndpoint = ({ target, path, endpoint }) => {
907
918
  const functionTypeName = `${pascalCase(endpoint.service)}Service_${pascalCase(functionName)}`;
908
919
  const functionTypeSource = generateArrowFunctionType({
909
920
  type: functionTypeName,
910
- params: ['id: string', `data: DeepPartial<${generateRequestBodyType(path).toString()}>`, 'options?: UpdateQuery', 'requestOptions?: RequestOptions'],
921
+ params: [
922
+ 'id: string',
923
+ `data: DeepPartial<${generateRequestBodyType(path).toString()}>`,
924
+ 'options?: UpdateQuery',
925
+ 'requestOptions?: RequestOptions'
926
+ ],
911
927
  returns: `${resolveResponseType(target)}<${generateResponseBodyType(path).toString()}>`
912
928
  });
913
929
  const functionSource = generateArrowFunction({
@@ -987,8 +1003,8 @@ const generateServices = (paths, entities, aliases, options) => {
987
1003
  for (const { path, endpoint } of paths) {
988
1004
  const generator = generators[endpoint.type];
989
1005
  for (const [method, config] of Object.entries(path)) {
990
- if ((method === 'get' && endpoint.type === WeclappEndpointType.ENTITY && !options.generateUnique)
991
- || (method === 'post' && (endpoint.type === WeclappEndpointType.COUNT || endpoint.path.endsWith('query')))) {
1006
+ if ((method === 'get' && endpoint.type === WeclappEndpointType.ENTITY && !options.generateUnique) ||
1007
+ (method === 'post' && (endpoint.type === WeclappEndpointType.COUNT || endpoint.path.endsWith('query')))) {
992
1008
  // Skip unique endpoints if generateUnique option is not set or if POST is used for filter queries
993
1009
  continue;
994
1010
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weclapp/sdk",
3
- "version": "2.0.0-dev.41",
4
- "description": "SDK generator based on a weclapp api swagger file",
3
+ "version": "2.0.0-dev.43",
4
+ "description": "weclapp SDK Generator",
5
5
  "author": "weclapp",
6
6
  "sideEffects": false,
7
7
  "bugs": "https://github.com/weclapp/sdk/issues",