swaggie 1.9.0-dev.1 → 2.0.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.
Files changed (46) hide show
  1. package/README.md +117 -31
  2. package/dist/browser.js +24 -5
  3. package/dist/cli.js +54 -7
  4. package/dist/gen/genMocks.js +453 -0
  5. package/dist/gen/genOperations.js +112 -20
  6. package/dist/gen/genTypes.js +6 -5
  7. package/dist/gen/header.js +0 -1
  8. package/dist/gen/index.js +14 -1
  9. package/dist/generated/bundledTemplates.js +17 -19
  10. package/dist/index.js +17 -1
  11. package/dist/swagger/operations.js +16 -7
  12. package/dist/swagger/typesExtractor.js +12 -11
  13. package/dist/types.d.ts +55 -5
  14. package/dist/utils/documentLoader.js +1 -3
  15. package/dist/utils/fileUtils.js +22 -0
  16. package/dist/utils/refResolver.js +9 -21
  17. package/dist/utils/templateEngine.js +18 -0
  18. package/dist/utils/templateManager.js +123 -14
  19. package/dist/utils/templateValidator.js +127 -0
  20. package/dist/utils/utils.js +19 -13
  21. package/package.json +5 -4
  22. package/templates/axios/operation.ejs +25 -21
  23. package/templates/fetch/operation.ejs +4 -0
  24. package/templates/ng1/operation.ejs +11 -3
  25. package/templates/ng2/baseClient.ejs +9 -49
  26. package/templates/ng2/client.ejs +3 -7
  27. package/templates/ng2/operation.ejs +34 -17
  28. package/templates/swr/baseClient.ejs +7 -0
  29. package/templates/swr/client.ejs +63 -0
  30. package/templates/swr/swrMutationOperation.ejs +32 -0
  31. package/templates/swr/swrOperation.ejs +18 -0
  32. package/templates/tsq/baseClient.ejs +1 -0
  33. package/templates/tsq/client.ejs +67 -0
  34. package/templates/tsq/mutationOperation.ejs +31 -0
  35. package/templates/tsq/queryOperation.ejs +19 -0
  36. package/templates/xior/operation.ejs +25 -21
  37. package/templates/swr-axios/barrel.ejs +0 -58
  38. package/templates/swr-axios/baseClient.ejs +0 -20
  39. package/templates/swr-axios/client.ejs +0 -21
  40. package/templates/swr-axios/operation.ejs +0 -40
  41. package/templates/swr-axios/swrOperation.ejs +0 -50
  42. package/templates/tsq-xior/barrel.ejs +0 -0
  43. package/templates/tsq-xior/baseClient.ejs +0 -14
  44. package/templates/tsq-xior/client.ejs +0 -22
  45. package/templates/tsq-xior/operation.ejs +0 -40
  46. package/templates/tsq-xior/queryOperation.ejs +0 -30
@@ -1,56 +1,16 @@
1
1
  import type { Observable } from "rxjs";
2
- import { Injectable, Inject, Optional, InjectionToken } from "@angular/core";
3
- import { HttpClient } from "@angular/common/http";
2
+ import { Injectable, InjectionToken, inject } from "@angular/core";
3
+ import { HttpClient, type HttpContext, type HttpHeaders, type HttpParams } from "@angular/common/http";
4
4
 
5
5
  export const <%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL = new InjectionToken<string>("<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL");
6
6
 
7
- abstract class BaseService {
8
- private httpClient: HttpClient;
9
- private baseUrl: string;
10
-
11
- constructor(
12
- @Inject(HttpClient) httpClient: HttpClient,
13
- @Optional() @Inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL) baseUrl?: string
14
- ) {
15
- this.httpClient = httpClient;
16
- this.baseUrl = baseUrl ? baseUrl : '';
17
- }
18
-
19
- protected $get<T>(url: string, options?: any): Observable<T> {
20
- return this.httpClient
21
- .get<T>(this.baseUrl + url, options)
22
- .pipe((response: any) => response);
23
- }
24
-
25
- protected $getAll<T>(url: string, options?: any): Observable<T[]> {
26
- return this.httpClient
27
- .get<T[]>(this.baseUrl + url, options)
28
- .pipe((response: any) => response);
29
- }
30
-
31
- protected $delete<T>(url: string, options?: any): Observable<T> {
32
- return this.httpClient
33
- .delete(this.baseUrl + url, options)
34
- .pipe((response: any) => response);
35
- }
36
-
37
- protected $post(url: string, data: any, options?: any): Observable<any> {
38
- return this.httpClient
39
- .post(this.baseUrl + url, data, options)
40
- .pipe((response: any) => response);
41
- }
42
-
43
- protected $patch<T>(url: string, data: any, options?: any): Observable<T> {
44
- return this.httpClient
45
- .patch(this.baseUrl + url, data, options)
46
- .pipe((response: any) => response);
47
- }
48
-
49
- protected $put(url: string, data: any, options?: any): Observable<any> {
50
- return this.httpClient
51
- .put(this.baseUrl + url, data, options)
52
- .pipe((response: any) => response);
53
- }
7
+ export interface HttpConfig {
8
+ headers?: HttpHeaders | Record<string, string | string[]>;
9
+ context?: HttpContext;
10
+ params?: HttpParams | Record<string, string | number | boolean | (string | number | boolean)[]>;
11
+ reportProgress?: boolean;
12
+ withCredentials?: boolean;
13
+ transferCache?: boolean | { includeHeaders?: string[] };
54
14
  }
55
15
 
56
16
  function paramsSerializer(params: any) {
@@ -1,13 +1,9 @@
1
1
  @Injectable({
2
2
  providedIn: 'root'
3
3
  })
4
- export class <%= it.clientName -%>Service extends BaseService {
5
- constructor(
6
- @Inject(HttpClient) httpClient: HttpClient,
7
- @Optional() @Inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL) baseUrl?: string
8
- ) {
9
- super(httpClient, baseUrl);
10
- }
4
+ export class <%= it.clientName.charAt(0).toUpperCase() + it.clientName.slice(1) -%>Service {
5
+ private http = inject(HttpClient);
6
+ private baseUrl = inject(<%= (it.servicePrefix || 'API').toUpperCase() -%>_BASE_URL, { optional: true }) ?? '';
11
7
 
12
8
  <% it.operations.forEach((operation) => { %>
13
9
  <%~ include('operation.ejs', operation); %>
@@ -4,23 +4,40 @@
4
4
  <% it.parameters.forEach((parameter) => { %>
5
5
  <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,
6
6
  <% }); %>
7
- config?: any
7
+ config?: HttpConfig
8
8
  ): Observable<<%~ it.returnType %>> {
9
- const url = `<%= it.url %>?<%
10
- if(it.query && it.query.length > 0) { %>${paramsSerializer({<%
11
- it.query.forEach((parameter) => { %>
12
- '<%= parameter.originalName %>': <%= parameter.name %>,
13
- <% }); %>})}<% } %>`;
14
-
15
- return this.$<%= it.method.toLowerCase() %>(
16
- url,
17
- <% if(['POST', 'PUT', 'PATCH'].includes(it.method)) { %>
18
- <% if(it.body) { %>
19
- <%= it.body.contentType === 'urlencoded' ? 'new URLSearchParams(' + it.body.name + ' as any)' : it.body.name %>,
20
- <% } else { %>
21
- null,
22
- <% } %>
9
+ <% if(it.query && it.query.length > 0) { -%>
10
+ const params = paramsSerializer({
11
+ <% it.query.forEach((parameter) => { -%>
12
+ <% if (it.queryParamObject) { -%>
13
+ '<%= parameter.originalName %>': <%= it.queryParamObject.name %>?.<%= parameter.name %>,
14
+ <% } else { -%>
15
+ '<%= parameter.originalName %>': <%= parameter.name %>,
16
+ <% } -%>
17
+ <% }); -%>
18
+ });
19
+ const url = `<%= it.url %>${params ? '?' + params : ''}`;
20
+ <% } else { -%>
21
+ const url = `<%= it.url %>`;
22
+ <% } -%>
23
+ <% if(it.headers && it.headers.length > 0) { it.headers.forEach((parameter) => { -%>
24
+ <% if (parameter.value) { -%>
25
+ config = { ...config, headers: { ...config?.headers, '<%= parameter.originalName %>': '<%= parameter.value %>' } };
26
+ <% } else { -%>
27
+ if (<%= parameter.name %>) {
28
+ config = { ...config, headers: { ...config?.headers, '<%= parameter.originalName %>': <%= parameter.name %> } };
29
+ }
30
+ <% } -%>
31
+ <% }); } -%>
32
+ <% if(it.method === 'GET') { -%>
33
+ return this.http.get<<%~ it.returnType %>>(this.baseUrl + url, config);
34
+ <% } else if(it.method === 'DELETE') { -%>
35
+ return this.http.delete<<%~ it.returnType %>>(this.baseUrl + url, config);
36
+ <% } else if(['POST', 'PUT', 'PATCH'].includes(it.method)) {
37
+ const bodyArg = it.body
38
+ ? (it.body.contentType === 'urlencoded' ? 'new URLSearchParams(' + it.body.name + ' as any)' : it.body.name)
39
+ : 'null';
40
+ -%>
41
+ return this.http.<%= it.method.toLowerCase() %><%~ '<' + it.returnType + '>' %>(this.baseUrl + url, <%~ bodyArg %>, config);
23
42
  <% } %>
24
- config
25
- );
26
43
  }
@@ -0,0 +1,7 @@
1
+ import useSWR, { type SWRConfiguration, type Key } from 'swr';
2
+ import useSWRMutation, { type SWRMutationConfiguration } from 'swr/mutation';
3
+
4
+ interface SwrConfig extends SWRConfiguration {
5
+ /* Custom key for SWR. You don't have to worry about this as by default it's the URL. You can use standard SWR Key here if you need more flexibility. */
6
+ key?: Key;
7
+ }
@@ -0,0 +1,63 @@
1
+ export const <%= it.camelCaseName -%>Client = {
2
+ <% it.operations.forEach((operation) => { %>
3
+ <%~ include('operation.ejs', operation); %>
4
+
5
+ <% }); %>
6
+ };
7
+
8
+ <%
9
+ var getOperations = it.operations.filter((o) => o.method === 'GET');
10
+ var mutationOperations = it.operations.filter((o) => o.method !== 'GET');
11
+
12
+ function toOpName(name) {
13
+ var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;
14
+ return n.charAt(0).toUpperCase() + n.slice(1);
15
+ }
16
+
17
+ function safeOperation(operation, clientName) {
18
+ var safeParams = operation.parameters.map(function(p) {
19
+ return p.name === clientName ? Object.assign({}, p, { name: '_' + p.name }) : p;
20
+ });
21
+ return Object.assign({}, operation, { parameters: safeParams });
22
+ }
23
+ %>
24
+
25
+ export const <%= it.camelCaseName %> = {
26
+ queries: {
27
+ <% getOperations.forEach((operation) => {
28
+ var opName = toOpName(operation.name);
29
+ var swrOperation = Object.assign({
30
+ swrOpName: 'use' + opName,
31
+ clientName: it.camelCaseName,
32
+ httpConfigType: it.httpConfigType,
33
+ }, safeOperation(operation, it.camelCaseName));
34
+ %>
35
+ <%~ include('swrOperation.ejs', swrOperation); %>
36
+
37
+ <% }); %>
38
+ },
39
+
40
+ mutations: {
41
+ <% mutationOperations.forEach((operation) => {
42
+ var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);
43
+ var swrMutationOperation = Object.assign({
44
+ mutOpName: 'use' + opName,
45
+ clientName: it.camelCaseName,
46
+ httpConfigType: it.httpConfigType,
47
+ }, safeOperation(operation, it.camelCaseName));
48
+ %>
49
+ <%~ include('swrMutationOperation.ejs', swrMutationOperation); %>
50
+
51
+ <% }); %>
52
+ },
53
+
54
+ queryKeys: {
55
+ <% getOperations.forEach((operation) => {
56
+ var opName = toOpName(operation.name);
57
+ var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);
58
+ var safeOp = safeOperation(operation, it.camelCaseName);
59
+ %>
60
+ <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => `<%= operation.url %><% if(safeOp.query && safeOp.query.length > 0) { %>?${encodeParams({<% safeOp.query.forEach((parameter) => { %>'<%= parameter.originalName %>': <%= safeOp.queryParamObject ? `${safeOp.queryParamObject.name}?.${parameter.name}` : parameter.name %>, <% }); %>})}<% } %>`,
61
+ <% }); %>
62
+ },
63
+ };
@@ -0,0 +1,32 @@
1
+ <%
2
+ var hasParams = it.parameters.length > 0;
3
+
4
+ var variablesType;
5
+ if (!hasParams) {
6
+ variablesType = 'void';
7
+ } else {
8
+ var parts = it.parameters.map(function(p) {
9
+ return p.name + (p.skippable ? '?' : '') + ': ' + p.type + (p.optional ? ' | null' : '');
10
+ });
11
+ variablesType = '{ ' + parts.join('; ') + ' }';
12
+ }
13
+
14
+ var callArgs = it.parameters.map(function(p) { return 'arg.' + p.name; });
15
+
16
+ // Stable SWR key: replace all ${...} path expressions with *
17
+ var swrKey = it.url.replace(/\$\{(?:[^{}]|\{[^{}]*\})*\}/g, '*');
18
+
19
+ var docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\n' : '';
20
+ %>
21
+ <%~ docs %>
22
+ <%= it.mutOpName %>(
23
+ $config?: SWRMutationConfiguration<<%~ it.returnType %>, Error, string, <%~ variablesType %>>,
24
+ $httpConfig?: <%= it.httpConfigType %>
25
+ ) {
26
+ return useSWRMutation<<%~ it.returnType %>, Error, string, <%~ variablesType %>>(
27
+ '<%= swrKey %>',
28
+ (_key: string, { arg }<%= hasParams ? ': { arg: ' + variablesType + ' }' : '' %>) =>
29
+ <%= it.clientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig).then((resp) => resp.data),
30
+ $config
31
+ );
32
+ },
@@ -0,0 +1,18 @@
1
+ <% var docs = it.jsDocs ? it.jsDocs.replace(/^/gm, ' ') + '\n' : ''; %>
2
+ <%~ docs %>
3
+ <%= it.swrOpName %>(
4
+ <% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,
5
+ <% }); %> $config?: Omit<SwrConfig, 'key'> & { key?: Key },
6
+ $httpConfig?: <%= it.httpConfigType %>
7
+ ) {
8
+ const { key, ...config } = $config || {};
9
+ const cacheUrl = key ?? <%= it.clientName %>.queryKeys.<%= it.swrOpName.charAt(3).toLowerCase() + it.swrOpName.slice(4) %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>);
10
+
11
+ const { data, error, isLoading, mutate } = useSWR<<%~ it.returnType %>>(
12
+ cacheUrl,
13
+ () => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig).then((resp) => resp.data),
14
+ config
15
+ );
16
+
17
+ return { data, isLoading, error, mutate };
18
+ },
@@ -0,0 +1 @@
1
+ import { type UseQueryOptions, type UseMutationOptions, useQuery, useMutation } from '@tanstack/react-query';
@@ -0,0 +1,67 @@
1
+ export const <%= it.camelCaseName %>Client = {
2
+ <% it.operations.forEach((operation) => { %>
3
+ <%~ include('operation.ejs', operation); %>
4
+
5
+ <% }); %>
6
+ };
7
+
8
+ <%
9
+ var getOperations = it.operations.filter((o) => o.method === 'GET');
10
+ var mutationOperations = it.operations.filter((o) => o.method !== 'GET');
11
+
12
+ // Helper: strip leading "get" and capitalise
13
+ function toOpName(name) {
14
+ var n = name.toLowerCase().startsWith('get') ? name.substring(3) : name;
15
+ return n.charAt(0).toUpperCase() + n.slice(1);
16
+ }
17
+
18
+ // Returns a deep copy of operation with any parameter whose name collides with
19
+ // the group object name (clientName) renamed to _<name> to avoid TS shadowing.
20
+ function safeOperation(operation, clientName) {
21
+ var safeParams = operation.parameters.map(function(p) {
22
+ return p.name === clientName ? Object.assign({}, p, { name: '_' + p.name }) : p;
23
+ });
24
+ return Object.assign({}, operation, { parameters: safeParams });
25
+ }
26
+ %>
27
+
28
+ export const <%= it.camelCaseName %> = {
29
+ queries: {
30
+ <% getOperations.forEach((operation) => {
31
+ var opName = toOpName(operation.name);
32
+ var queryOperation = Object.assign({
33
+ rqOpName: 'use' + opName,
34
+ opKey: it.camelCaseName + opName,
35
+ clientName: it.camelCaseName,
36
+ httpConfigType: it.httpConfigType,
37
+ }, safeOperation(operation, it.camelCaseName));
38
+ %>
39
+ <%~ include('queryOperation.ejs', queryOperation); %>
40
+
41
+ <% }); %>
42
+ },
43
+
44
+ mutations: {
45
+ <% mutationOperations.forEach((operation) => {
46
+ var opName = operation.name.charAt(0).toUpperCase() + operation.name.slice(1);
47
+ var mutationOperation = Object.assign({
48
+ mutOpName: 'use' + opName,
49
+ clientName: it.camelCaseName,
50
+ httpConfigType: it.httpConfigType,
51
+ }, safeOperation(operation, it.camelCaseName));
52
+ %>
53
+ <%~ include('mutationOperation.ejs', mutationOperation); %>
54
+
55
+ <% }); %>
56
+ },
57
+
58
+ queryKeys: {
59
+ <% getOperations.forEach((operation) => {
60
+ var opName = toOpName(operation.name);
61
+ var keyName = opName.charAt(0).toLowerCase() + opName.slice(1);
62
+ var safeOp = safeOperation(operation, it.camelCaseName);
63
+ %>
64
+ <%= keyName %>: (<% safeOp.parameters.forEach((parameter) => { %><%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>, <% }); %>) => ['<%= it.camelCaseName %>', '<%= it.camelCaseName + opName %>'<% safeOp.parameters.forEach((parameter) => { %>, <%= parameter.name %><% }); %>] as const,
65
+ <% }); %>
66
+ },
67
+ };
@@ -0,0 +1,31 @@
1
+ <%
2
+ var hasParams = it.parameters.length > 0;
3
+
4
+ var variablesType;
5
+ if (!hasParams) {
6
+ variablesType = 'void';
7
+ } else {
8
+ var parts = it.parameters.map(function(p) {
9
+ return p.name + (p.skippable ? '?' : '') + ': ' + p.type + (p.optional ? ' | null' : '');
10
+ });
11
+ variablesType = '{ ' + parts.join('; ') + ' }';
12
+ }
13
+
14
+ var callArgs = it.parameters.map(function(p) { return 'vars.' + p.name; });
15
+
16
+ var baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\n * @param $httpConfig (optional) Additional HTTP client configuration (passed to the underlying ' + it.httpConfigType + ')';
17
+ var rawDocs = it.jsDocs
18
+ ? it.jsDocs.replace(/(\s*)\*\/\s*$/, '\n' + baseAdditionalParams + '\n */')
19
+ : '';
20
+ var docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\n' : '';
21
+ %>
22
+ <%~ docs %>
23
+ <%= it.mutOpName %><TData = <%~ it.returnType %>, TError = Error>(
24
+ $config?: UseMutationOptions<<%~ it.returnType %>, TError, <%~ variablesType %>>,
25
+ $httpConfig?: <%= it.httpConfigType %>
26
+ ) {
27
+ return useMutation<<%~ it.returnType %>, TError, <%~ variablesType %>>({
28
+ mutationFn: (<%= hasParams ? 'vars' : '' %>) => <%= it.clientName %>Client.<%= it.name %>(<%= callArgs.join(', ') %><%= callArgs.length > 0 ? ', ' : '' %>$httpConfig).then(res => res.data),
29
+ ...$config
30
+ });
31
+ },
@@ -0,0 +1,19 @@
1
+ <%
2
+ var baseAdditionalParams = ' * @param $config (optional) Additional configuration for TanStack Query\n * @param $httpConfig (optional) Additional HTTP client configuration (passed to the underlying ' + it.httpConfigType + ')';
3
+ var rawDocs = it.jsDocs
4
+ ? it.jsDocs.replace(/(\s*)\*\/\s*$/, '\n' + baseAdditionalParams + '\n */')
5
+ : '';
6
+ var docs = rawDocs ? rawDocs.replace(/^/gm, ' ') + '\n' : '';
7
+ %>
8
+ <%~ docs %>
9
+ <%= it.rqOpName %><TData = <%~ it.returnType %>, TError = Error>(
10
+ <% it.parameters.forEach((parameter) => { %> <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %><%= parameter.optional ? (parameter.skippable ? ' | null' : ' | null | undefined') : '' %>,
11
+ <% }); %> $config?: Omit<UseQueryOptions<<%~ it.returnType %>, TError, TData>, 'queryKey' | 'queryFn'>,
12
+ $httpConfig?: <%= it.httpConfigType %>
13
+ ) {
14
+ return useQuery<<%~ it.returnType %>, TError, TData>({
15
+ queryKey: <%= it.clientName %>.queryKeys.<%= it.rqOpName.charAt(3).toLowerCase() + it.rqOpName.slice(4) %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>),
16
+ queryFn: () => <%= it.clientName %>Client.<%= it.name %>(<% it.parameters.forEach((parameter) => { %><%= parameter.name %>, <% }); %>$httpConfig).then(res => res.data),
17
+ ...$config
18
+ });
19
+ },
@@ -10,31 +10,35 @@ $config?: XiorRequestConfig
10
10
  return http.request<<%~ it.returnType %>>({
11
11
  url: url,
12
12
  method: '<%= it.method %>',
13
- <% if(it.body) { %>
14
- <% if(it.body.contentType === 'urlencoded') { %>
13
+ <% if(it.body) { -%>
14
+ <% if(it.body.contentType === 'urlencoded') { -%>
15
15
  data: new URLSearchParams(<%= it.body.name %> as any),
16
- <% } else { %>
16
+ <% } else { -%>
17
17
  data: <%= it.body.name %>,
18
- <% } %>
19
- <% } %>
20
- <% if(it.query && it.query.length > 0) { %>
18
+ <% } -%>
19
+ <% } -%>
20
+ <% if(it.query && it.query.length > 0) { -%>
21
21
  params: {
22
- <% it.query.forEach((parameter) => { %>
23
- '<%= parameter.originalName %>': <%= parameter.name %>,
24
- <% }); %>
25
- },
26
- <% } %>
27
- <% if(it.headers && it.headers.length > 0) { %>
22
+ <% it.query.forEach((parameter) => { -%>
23
+ <% if (it.queryParamObject) { -%>
24
+ '<%= parameter.originalName %>': <%= it.queryParamObject.name %>?.<%= parameter.name %>,
25
+ <% } else { -%>
26
+ '<%= parameter.originalName %>': <%= parameter.name %>,
27
+ <% } -%>
28
+ <% }); -%>
29
+ },
30
+ <% } -%>
31
+ <% if(it.headers && it.headers.length > 0) { -%>
28
32
  headers: {
29
- <% it.headers.forEach((parameter) => { %>
30
- <% if (parameter.value) { %>
31
- '<%= parameter.originalName %>': '<%= parameter.value %>',
32
- <% } else { %>
33
- '<%= parameter.originalName %>': <%= parameter.name %>,
34
- <% } %>
35
- <% }); %>
36
- },
37
- <% } %>
33
+ <% it.headers.forEach((parameter) => { -%>
34
+ <% if (parameter.value) { -%>
35
+ '<%= parameter.originalName %>': '<%= parameter.value %>',
36
+ <% } else { -%>
37
+ '<%= parameter.originalName %>': <%= parameter.name %>,
38
+ <% } -%>
39
+ <% }); -%>
40
+ },
41
+ <% } -%>
38
42
  ...$config,
39
43
  });
40
44
  },
@@ -1,58 +0,0 @@
1
-
2
- /**
3
- * Serializes a params object into a query string that is compatible with different REST APIs.
4
- * Implementation from: https://github.com/suhaotian/xior/blob/main/src/utils.ts
5
- * Kudos to @suhaotian for the original implementation
6
- */
7
- function encodeParams<T = any>(
8
- params: T,
9
- parentKey: string | null = null,
10
- options?: {
11
- allowDots?: boolean;
12
- serializeDate?: (value: Date) => string;
13
- arrayFormat?: 'indices' | 'repeat' | 'brackets';
14
- }
15
- ): string {
16
- if (params === undefined || params === null) return '';
17
- const encodedParams: string[] = [];
18
- const paramsIsArray = Array.isArray(params);
19
- const { arrayFormat, allowDots, serializeDate } = options || {};
20
-
21
- const getKey = (key: string) => {
22
- if (allowDots && !paramsIsArray) return `.${key}`;
23
- if (paramsIsArray) {
24
- if (arrayFormat === 'brackets') {
25
- return '[]';
26
- }
27
- if (arrayFormat === 'repeat') {
28
- return '';
29
- }
30
- }
31
- return `[${key}]`;
32
- };
33
-
34
- for (const key in params) {
35
- if (Object.prototype.hasOwnProperty.call(params, key)) {
36
- let value = (params as any)[key];
37
- if (value !== undefined) {
38
- const encodedKey = parentKey ? `${parentKey}${getKey(key)}` : (key as string);
39
-
40
- // biome-ignore lint/suspicious/noGlobalIsNan: <explanation>
41
- if (!isNaN(value) && value instanceof Date) {
42
- value = serializeDate ? serializeDate(value) : value.toISOString();
43
- }
44
- if (typeof value === 'object') {
45
- // If the value is an object or array, recursively encode its contents
46
- const result = encodeParams(value, encodedKey, options);
47
- if (result !== '') encodedParams.push(result);
48
- } else {
49
- // Otherwise, encode the key-value pair
50
- encodedParams.push(`${encodeURIComponent(encodedKey)}=${encodeURIComponent(value)}`);
51
- }
52
- }
53
- }
54
- }
55
-
56
- return encodedParams.join('&');
57
- }
58
-
@@ -1,20 +0,0 @@
1
- import Axios, { type AxiosPromise, type AxiosRequestConfig } from "axios";
2
- import useSWR, { type SWRConfiguration, type Key } from 'swr';
3
-
4
- export const axios = Axios.create({
5
- baseURL: '<%= it.baseUrl || '' -%>',
6
- paramsSerializer: (params: any) =>
7
- encodeParams(params, null, {
8
- allowDots: <%= it.allowDots %>,
9
- arrayFormat: '<%= it.arrayFormat %>',
10
- }),
11
- });
12
-
13
- interface SwrConfig extends SWRConfiguration {
14
- /* Custom key for SWR. You don't have to worry about this as by default it's the URL. You can use standard SWR Key here if you need more flexibility. */
15
- key?: Key;
16
-
17
- /* Configuration for axios fetcher */
18
- axios?: AxiosRequestConfig;
19
- }
20
-
@@ -1,21 +0,0 @@
1
- export const <%= it.camelCaseName -%>Client = {
2
- <% it.operations.forEach((operation) => { %>
3
- <%~ include('operation.ejs', operation); %>
4
-
5
- <% }); %>
6
- };
7
-
8
- <% var getOperations = it.operations.filter((o) => o.method === 'GET');
9
- if(getOperations.length > 0) { %>
10
- <% getOperations.forEach((operation) => {
11
- var opName = operation.name;
12
- if(opName.toLowerCase().startsWith("get")) {
13
- opName = opName.substring(3);
14
- }
15
- opName[0] = opName[0].toUpperCase();
16
- var customName = "use" + it.clientName + opName;
17
- var swrOperation = Object.assign({ swrOpName: customName }, operation); %>
18
- <%~ include('swrOperation.ejs', swrOperation); %>
19
-
20
- <% }); %>
21
- <% } %>
@@ -1,40 +0,0 @@
1
- <%~ it.jsDocs %>
2
-
3
- <%= it.name %>(<% it.parameters.forEach((parameter) => { %>
4
- <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,
5
- <% }); %>
6
- $config?: AxiosRequestConfig
7
- ): AxiosPromise<<%~ it.returnType %>> {
8
- const url = `<%= it.url %>`;
9
-
10
- return axios.request<<%~ it.returnType %>>({
11
- url: url,
12
- method: '<%= it.method %>',
13
- <% if(it.body) { %>
14
- <% if(it.body.contentType === 'urlencoded') { %>
15
- data: new URLSearchParams(<%= it.body.name %> as any),
16
- <% } else { %>
17
- data: <%= it.body.name %>,
18
- <% } %>
19
- <% } %>
20
- <% if(it.query && it.query.length > 0) { %>
21
- params: {
22
- <% it.query.forEach((parameter) => { %>
23
- '<%= parameter.originalName %>': <%= parameter.name %>,
24
- <% }); %>
25
- },
26
- <% } %>
27
- <% if(it.headers && it.headers.length > 0) { %>
28
- headers: {
29
- <% it.headers.forEach((parameter) => { %>
30
- <% if (parameter.value) { %>
31
- '<%= parameter.originalName %>': '<%= parameter.value %>',
32
- <% } else { %>
33
- '<%= parameter.originalName %>': <%= parameter.name %>,
34
- <% } %>
35
- <% }); %>
36
- },
37
- <% } %>
38
- ...$config,
39
- });
40
- },
@@ -1,50 +0,0 @@
1
- <%~ it.jsDocs %>
2
-
3
- export function <%= it.swrOpName %>(<% it.parameters.forEach((parameter) => { %>
4
- <%= parameter.name %><%= parameter.skippable ? '?' : '' %>: <%~ parameter.type %> <%= parameter.optional ? (parameter.skippable ? '| null' : '| null | undefined') : '' %>,
5
- <% }); %>
6
- $config?: SwrConfig
7
- ) {
8
- const url = `<%= it.url %>`;
9
- const { axios: $axiosConf, key, ...config } = $config || {};
10
-
11
- const cacheUrl = `${url}?<%
12
- if(it.query && it.query.length > 0) { %>${encodeParams({<%
13
- it.query.forEach((parameter) => { %>
14
- '<%= parameter.originalName %>': <%= parameter.name %>,
15
- <% }); %>})}<% } %>`;
16
-
17
- const { data, error, mutate } = useSWR<<%~ it.returnType %>>(
18
- key ?? cacheUrl,
19
- () => axios.request({
20
- url: url,
21
- method: '<%= it.method %>',
22
- <% if(it.query && it.query.length > 0) { %>
23
- params: {
24
- <% it.query.forEach((parameter) => { %>
25
- '<%= parameter.originalName %>': <%= parameter.name %>,
26
- <% }); %>
27
- },
28
- <% } %>
29
- <% if(it.headers && it.headers.length > 0) { %>
30
- headers: {
31
- <% it.headers.forEach((parameter) => { %>
32
- <% if (parameter.value) { %>
33
- '<%= parameter.originalName %>': '<%= parameter.value %>',
34
- <% } else { %>
35
- '<%= parameter.originalName %>': <%= parameter.name %>,
36
- <% } %>
37
- <% }); %>
38
- },
39
- <% } %>
40
- ...$axiosConf})
41
- .then((resp) => resp.data),
42
- config);
43
-
44
- return {
45
- data,
46
- isLoading: !error && !data,
47
- error: error,
48
- mutate,
49
- };
50
- }
File without changes