@naiv/codegen-axios-client 0.0.14 → 0.0.16

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.
@@ -11,31 +11,87 @@ function buildAPIClient(list_api) {
11
11
  };
12
12
  }
13
13
  function createAPIClientNamespace(list_api) {
14
- const list_api_import = list_api.filter((api) => api.data.alias).map((api) => [
15
- `import { T_${api.data.alias} } from "./api/${api.data.alias}";`,
16
- ].join('\n'));
17
- const list_api_str = list_api.filter((api) => api.data.alias).map((api) => {
18
- var _a, _b, _c, _d;
14
+ const list_api_import = list_api.filter((api) => api.data.alias).map((api) => {
15
+ const is_streaming = api.data.return_type.kind === 'stream';
16
+ const prefix = is_streaming ? 'S_' : 'T_';
19
17
  return [
20
- ` export const ${api.data.alias}: T_${api.data.alias} = async (req, base_url: string = BaseURL.instance.base_url) => {`,
21
- ` const final_url = __build_path(base_url, '${api.url_path}', ${(((_b = (_a = api.data.path) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? 'req.path' : '{}'});`,
22
- (['get', 'delete'].includes(api.method)
23
- ? ` return (await axios['${api.method.toLowerCase()}'](final_url, ${createAPIParams(api)})).data as any;`
24
- : ` return (await axios['${api.method.toLowerCase()}'](final_url, ${((_d = (_c = api.data.body) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0 ? 'req.body' : '{}'}, ${createAPIParams(api)})).data as any;`),
25
- ` }`,
18
+ `import { ${prefix}${api.data.alias} } from "./api/${api.data.alias}";`,
26
19
  ].join('\n');
27
20
  });
21
+ const list_api_str = list_api.filter((api) => api.data.alias).map((api) => {
22
+ var _a, _b, _c, _d, _e, _f, _g, _h;
23
+ const is_streaming = api.data.return_type.kind === 'stream';
24
+ const prefix = is_streaming ? 'S_' : 'T_';
25
+ if (is_streaming) {
26
+ return [
27
+ ` export const ${api.data.alias}: ${prefix}${api.data.alias} = (req, base_url: string = BaseURL.instance.base_url) => {`,
28
+ ` const final_url = __build_path(base_url, '${api.url_path}', ${(((_b = (_a = api.data.path) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) ? 'req.path' : '{}'});`,
29
+ ` const ac = new AbortController();`,
30
+ ``,
31
+ ` return {`,
32
+ ` stream: async (onMessage) => {`,
33
+ (['get', 'delete'].includes(api.method)
34
+ ? ` const response = await axios['${api.method.toLowerCase()}'](final_url, ${createAPIParams(api, true)});`
35
+ : ` const response = await axios['${api.method.toLowerCase()}'](final_url, ${((_d = (_c = api.data.body) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0 ? 'req.body' : '{}'}, ${createAPIParams(api, true)});`),
36
+ ` for await (const chunk of response.data) {`,
37
+ ` const data = chunk.toString('utf-8');`,
38
+ (() => {
39
+ switch (api.data.return_type.type.type) {
40
+ case "native":
41
+ switch (api.data.return_type.type.type_native) {
42
+ case 'string':
43
+ return ` onMessage(data, false);`;
44
+ case 'number':
45
+ return ` onMessage(Number(data), false);`;
46
+ case 'boolean':
47
+ return ` onMessage(data === 'true', false);`;
48
+ }
49
+ case "enum":
50
+ return ` onMessage(data, false);`;
51
+ case "table":
52
+ case "schema":
53
+ case "inline-schema":
54
+ return ` onMessage(JSON.parse(data), false);`;
55
+ }
56
+ })(),
57
+ ` }`,
58
+ ` onMessage(null as any, true);`,
59
+ ` },`,
60
+ ` cancel() {`,
61
+ ` ac.abort();`,
62
+ ` }`,
63
+ ` }`,
64
+ ` }`,
65
+ ].join('\n');
66
+ }
67
+ else {
68
+ return [
69
+ ` export const ${api.data.alias}: ${prefix}${api.data.alias} = async (req, base_url: string = BaseURL.instance.base_url) => {`,
70
+ ` const final_url = __build_path(base_url, '${api.url_path}', ${(((_f = (_e = api.data.path) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : 0) > 0) ? 'req.path' : '{}'});`,
71
+ (['get', 'delete'].includes(api.method)
72
+ ? ` return (await axios['${api.method.toLowerCase()}'](final_url, ${createAPIParams(api)})).data as any;`
73
+ : ` return (await axios['${api.method.toLowerCase()}'](final_url, ${((_h = (_g = api.data.body) === null || _g === void 0 ? void 0 : _g.length) !== null && _h !== void 0 ? _h : 0) > 0 ? 'req.body' : '{}'}, ${createAPIParams(api)})).data as any;`),
74
+ ` }`,
75
+ ].join('\n');
76
+ }
77
+ });
28
78
  return [
29
79
  `import axios from 'axios';`,
30
80
  list_api_import.join('\n'),
31
81
  '',
82
+ 'export type OnMessage<T> = (chunk: T, is_complete: boolean) => void;',
83
+ 'export interface StreamResponse<T> {',
84
+ ' cancel(): void',
85
+ ' stream(onMessage: OnMessage<T>): Promise<void>',
86
+ '}',
87
+ '',
32
88
  'export namespace AxiosClient {',
33
89
  utility_functions,
34
90
  list_api_str.join('\n'),
35
91
  '}'
36
92
  ].join('\n');
37
93
  }
38
- function createAPIParams(api) {
94
+ function createAPIParams(api, with_stream_header = false) {
39
95
  var _a, _b, _c, _d;
40
96
  let result = '{';
41
97
  if (((_b = (_a = api.data.headers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) {
@@ -44,6 +100,10 @@ function createAPIParams(api) {
44
100
  if (((_d = (_c = api.data.query) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0) {
45
101
  result = `${result} params: req.query as any,`;
46
102
  }
103
+ if (with_stream_header) {
104
+ result = `${result} responseType: 'stream',`;
105
+ result = `${result} signal: ac.signal,`;
106
+ }
47
107
  result = `${result} }`;
48
108
  return result;
49
109
  }
@@ -12,13 +12,15 @@ function getAPIFileName(api_alias, api, extension) {
12
12
  }
13
13
  function buildFromAPI(api, list_schema) {
14
14
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1;
15
+ const is_streaming = api.data.return_type.kind === 'stream';
16
+ const prefix = is_streaming ? 'S_' : 'T_';
15
17
  if (!api.data.alias) {
16
18
  throw new Error(`no api alias`);
17
19
  }
18
20
  let header_schema;
19
21
  if (((_b = (_a = api.data.headers) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 0) {
20
22
  const s = {
21
- name: `T_${api.data.alias}_headers`,
23
+ name: `${prefix}${api.data.alias}_headers`,
22
24
  items: (_c = api.data.headers) !== null && _c !== void 0 ? _c : []
23
25
  };
24
26
  header_schema = (0, build_from_schema_1.getSchemaContent)(s, list_schema);
@@ -26,7 +28,7 @@ function buildFromAPI(api, list_schema) {
26
28
  let query_schema;
27
29
  if (((_e = (_d = api.data.query) === null || _d === void 0 ? void 0 : _d.length) !== null && _e !== void 0 ? _e : 0) > 0) {
28
30
  const s = {
29
- name: `T_${api.data.alias}_query`,
31
+ name: `${prefix}${api.data.alias}_query`,
30
32
  items: (_f = api.data.query) !== null && _f !== void 0 ? _f : []
31
33
  };
32
34
  query_schema = (0, build_from_schema_1.getSchemaContent)(s, list_schema);
@@ -34,7 +36,7 @@ function buildFromAPI(api, list_schema) {
34
36
  let path_schema;
35
37
  if (((_h = (_g = api.data.path) === null || _g === void 0 ? void 0 : _g.length) !== null && _h !== void 0 ? _h : 0) > 0) {
36
38
  const s = {
37
- name: `T_${api.data.alias}_path`,
39
+ name: `${prefix}${api.data.alias}_path`,
38
40
  items: (_j = api.data.path) !== null && _j !== void 0 ? _j : []
39
41
  };
40
42
  path_schema = (0, build_from_schema_1.getSchemaContent)(s, list_schema);
@@ -42,7 +44,7 @@ function buildFromAPI(api, list_schema) {
42
44
  let body_schema;
43
45
  if (((_l = (_k = api.data.body) === null || _k === void 0 ? void 0 : _k.length) !== null && _l !== void 0 ? _l : 0) > 0) {
44
46
  const s = {
45
- name: `T_${api.data.alias}_body`,
47
+ name: `${prefix}${api.data.alias}_body`,
46
48
  items: (_m = api.data.body) !== null && _m !== void 0 ? _m : []
47
49
  };
48
50
  body_schema = (0, build_from_schema_1.getSchemaContent)(s, list_schema);
@@ -81,18 +83,23 @@ ${[
81
83
  ...fake_return_type_field_result.new_class_lines,
82
84
  ].join('\n')}
83
85
 
84
- export type T_${api.data.alias} = (request: {
86
+ ${is_streaming ? `import { OnMessage, StreamResponse } from '../AxiosClient';` : ''}
87
+
88
+ export type ${prefix}${api.data.alias} = (request: {
85
89
  ${[
86
- ((_y = header_schema === null || header_schema === void 0 ? void 0 : header_schema.content.length) !== null && _y !== void 0 ? _y : 0) > 0 ? ` headers: T_${api.data.alias}_headers` : '',
87
- ((_z = query_schema === null || query_schema === void 0 ? void 0 : query_schema.content.length) !== null && _z !== void 0 ? _z : 0) > 0 ? ` query: T_${api.data.alias}_query` : '',
88
- ((_0 = path_schema === null || path_schema === void 0 ? void 0 : path_schema.content.length) !== null && _0 !== void 0 ? _0 : 0) > 0 ? ` path: T_${api.data.alias}_path` : '',
89
- ((_1 = body_schema === null || body_schema === void 0 ? void 0 : body_schema.content.length) !== null && _1 !== void 0 ? _1 : 0) > 0 ? ` body: T_${api.data.alias}_body` : '',
90
+ ((_y = header_schema === null || header_schema === void 0 ? void 0 : header_schema.content.length) !== null && _y !== void 0 ? _y : 0) > 0 ? ` headers: ${prefix}${api.data.alias}_headers` : '',
91
+ ((_z = query_schema === null || query_schema === void 0 ? void 0 : query_schema.content.length) !== null && _z !== void 0 ? _z : 0) > 0 ? ` query: ${prefix}${api.data.alias}_query` : '',
92
+ ((_0 = path_schema === null || path_schema === void 0 ? void 0 : path_schema.content.length) !== null && _0 !== void 0 ? _0 : 0) > 0 ? ` path: ${prefix}${api.data.alias}_path` : '',
93
+ ((_1 = body_schema === null || body_schema === void 0 ? void 0 : body_schema.content.length) !== null && _1 !== void 0 ? _1 : 0) > 0 ? ` body: ${prefix}${api.data.alias}_body` : '',
90
94
  ].filter(Boolean).join('\n')}
91
- }, base_url?: string) => Promise<${fake_return_type_field_result.type_only}${api.data.return_type.type.required ? '' : ' | null'}>;
95
+ ${is_streaming
96
+ ? `}, base_url?: string) => StreamResponse<${fake_return_type_field_result.type_only}${api.data.return_type.type.required ? '' : ' | null'}>;`
97
+ : `}, base_url?: string) => Promise<${fake_return_type_field_result.type_only}${api.data.return_type.type.required ? '' : ' | null'}>;`}
92
98
 
93
99
  export const method = '${api.method.toLowerCase()}';
94
100
  export const url_path = '${api.url_path}';
95
101
  export const alias = '${api.data.alias}';
102
+ export const is_streaming = ${is_streaming ? 'true' : 'false'};
96
103
  `
97
104
  ].join('\n')
98
105
  }],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naiv/codegen-axios-client",
3
- "version": "0.0.14",
3
+ "version": "0.0.16",
4
4
  "main": "dist/index.js",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -28,7 +28,7 @@
28
28
  "typescript": "^5.6.3"
29
29
  },
30
30
  "dependencies": {
31
- "@naiv/core": "^0.0.14",
31
+ "@naiv/core": "^0.0.15",
32
32
  "arg": "^5.0.2",
33
33
  "lodash": "^4.17.21"
34
34
  }