@waywake/youzanyun-sdk 2.0.0 → 2.0.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.
@@ -2,10 +2,11 @@
2
2
  * API 调用客户端
3
3
  */
4
4
  import type { ApiCallParams } from './types';
5
+ import * as utilHttp from './utils/http';
5
6
  /**
6
7
  * 发起接口调用
7
8
  *
8
9
  * @param apiParam 接口调用参数 { api, version, token?, params?, files?, config?, host? }
9
10
  */
10
- export declare function call(apiParam: ApiCallParams): Promise<import("axios").AxiosResponse<any, any, {}>>;
11
+ export declare function call(apiParam: ApiCallParams): Promise<utilHttp.HttpResponse<unknown>>;
11
12
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAa7C;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,QAAQ,EAAE,aAAa,wDAgC3C"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,OAAO,KAAK,QAAQ,MAAM,cAAc,CAAC;AAWzC;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,QAAQ,EAAE,aAAa,2CAgC3C"}
package/dist/cjs/index.js CHANGED
@@ -98,35 +98,77 @@ function getUrlTextArea(api, version) {
98
98
 
99
99
  // src/utils/http.ts
100
100
  var import_fs = __toESM(require("fs"));
101
- var import_axios = __toESM(require("axios"));
102
- var import_form_data = __toESM(require("form-data"));
103
- var httpClient = import_axios.default.create({
104
- baseURL: getBaseUrl(),
105
- headers: {
106
- "User-Agent": "YZY-Open-Client 1.0.0 - Node"
101
+ var import_path = __toESM(require("path"));
102
+ var USER_AGENT = "YZY-Open-Client 1.0.0 - Node";
103
+
104
+ class HttpError extends Error {
105
+ response;
106
+ constructor(response) {
107
+ super(`Request failed with status code ${response.status}`);
108
+ this.name = "HttpError";
109
+ this.response = response;
107
110
  }
108
- });
111
+ }
112
+ function getRequestUrl(url) {
113
+ return new URL(url, getBaseUrl()).toString();
114
+ }
115
+ function headersToObject(headers) {
116
+ const result = {};
117
+ headers.forEach((value, key) => {
118
+ result[key] = value;
119
+ });
120
+ return result;
121
+ }
122
+ async function parseResponseBody(resp) {
123
+ const text = await resp.text();
124
+ if (text === "") {
125
+ return null;
126
+ }
127
+ const contentType = resp.headers.get("content-type");
128
+ if (contentType?.includes("application/json")) {
129
+ return JSON.parse(text);
130
+ }
131
+ return text;
132
+ }
133
+ async function toHttpResponse(resp) {
134
+ const response = {
135
+ data: await parseResponseBody(resp),
136
+ status: resp.status,
137
+ statusText: resp.statusText,
138
+ headers: headersToObject(resp.headers),
139
+ url: resp.url
140
+ };
141
+ if (!resp.ok) {
142
+ throw new HttpError(response);
143
+ }
144
+ return response;
145
+ }
109
146
  async function post(url, params) {
110
- const resp = await httpClient.post(url, params, {
111
- headers: { "Content-type": "application/json;charset=UTF-8" }
147
+ const resp = await fetch(getRequestUrl(url), {
148
+ method: "POST",
149
+ headers: {
150
+ "User-Agent": USER_AGENT,
151
+ "Content-type": "application/json;charset=UTF-8"
152
+ },
153
+ body: params === undefined ? undefined : JSON.stringify(params)
112
154
  });
113
- return resp;
155
+ return toHttpResponse(resp);
114
156
  }
115
157
  async function upload(url, files) {
116
- const form = new import_form_data.default;
117
- if (files instanceof Map) {
118
- files.forEach((filePath, key) => {
119
- form.append(key, import_fs.default.createReadStream(filePath));
120
- });
121
- } else {
122
- for (const [key, filePath] of Object.entries(files)) {
123
- form.append(key, import_fs.default.createReadStream(filePath));
124
- }
158
+ const form = new FormData;
159
+ for (const [key, filePath] of files instanceof Map ? files : Object.entries(files)) {
160
+ const data = await import_fs.default.promises.readFile(filePath);
161
+ const blob = new Blob([new Uint8Array(data)]);
162
+ form.append(key, blob, import_path.default.basename(filePath));
125
163
  }
126
- const resp = await httpClient.post(url, form, {
127
- headers: form.getHeaders()
164
+ const resp = await fetch(getRequestUrl(url), {
165
+ method: "POST",
166
+ headers: {
167
+ "User-Agent": USER_AGENT
168
+ },
169
+ body: form
128
170
  });
129
- return resp;
171
+ return toHttpResponse(resp);
130
172
  }
131
173
 
132
174
  // src/client.ts
@@ -2,6 +2,7 @@
2
2
  * Token 管理
3
3
  */
4
4
  import type { TokenParams } from './types';
5
+ import * as utilHttp from './utils/http';
5
6
  /**
6
7
  * 获取 Token
7
8
  *
@@ -11,5 +12,5 @@ import type { TokenParams } from './types';
11
12
  * 工具型应用获取 Token: authorize_type = authorization_code, 必传 code redirect_uri
12
13
  * 工具型应用/自用型应用刷新 Token: authorize_type = refresh_token, 必传 refresh_token
13
14
  */
14
- export declare function get(params: TokenParams): Promise<import("axios").AxiosResponse<any, any, {}>>;
15
+ export declare function get(params: TokenParams): Promise<utilHttp.HttpResponse<unknown>>;
15
16
  //# sourceMappingURL=token.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/token.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAU3C;;;;;;;;GAQG;AACH,wBAAgB,GAAG,CAAC,MAAM,EAAE,WAAW,wDA4CtC"}
1
+ {"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/token.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,KAAK,QAAQ,MAAM,cAAc,CAAC;AAQzC;;;;;;;;GAQG;AACH,wBAAgB,GAAG,CAAC,MAAM,EAAE,WAAW,2CA4CtC"}
@@ -1,18 +1,29 @@
1
1
  /**
2
2
  * HTTP 请求工具
3
3
  */
4
+ export interface HttpResponse<T = unknown> {
5
+ data: T;
6
+ status: number;
7
+ statusText: string;
8
+ headers: Record<string, string>;
9
+ url: string;
10
+ }
11
+ export declare class HttpError<T = unknown> extends Error {
12
+ response: HttpResponse<T>;
13
+ constructor(response: HttpResponse<T>);
14
+ }
4
15
  /**
5
16
  * 发起 POST 请求
6
17
  *
7
18
  * @param url 支持绝对路径、相对路径
8
19
  * @param params POST 参数
9
20
  */
10
- export declare function post(url: string, params?: Record<string, unknown>): Promise<import("axios").AxiosResponse<any, any, {}>>;
21
+ export declare function post(url: string, params?: Record<string, unknown>): Promise<HttpResponse<unknown>>;
11
22
  /**
12
23
  * 发起上传文件请求
13
24
  *
14
25
  * @param url 支持绝对路径、相对路径
15
26
  * @param files 上传文件参数,支持 Map 或 Object。示例: {"image": "/path/to/filename.jpg"}
16
27
  */
17
- export declare function upload(url: string, files: Map<string, string> | Record<string, string>): Promise<import("axios").AxiosResponse<any, any, {}>>;
28
+ export declare function upload(url: string, files: Map<string, string> | Record<string, string>): Promise<HttpResponse<unknown>>;
18
29
  //# sourceMappingURL=http.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/utils/http.ts"],"names":[],"mappings":"AAAA;;GAEG;AAeH;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,wDAKvE;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,wDAiB5F"}
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/utils/http.ts"],"names":[],"mappings":"AAAA;;GAEG;AAQH,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,OAAO;IACvC,IAAI,EAAE,CAAC,CAAC;IACR,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,GAAG,EAAE,MAAM,CAAC;CACb;AAED,qBAAa,SAAS,CAAC,CAAC,GAAG,OAAO,CAAE,SAAQ,KAAK;IAC/C,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;gBAEd,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC;CAKtC;AA4CD;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,kCAUvE;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,kCAiB5F"}
@@ -2,10 +2,11 @@
2
2
  * API 调用客户端
3
3
  */
4
4
  import type { ApiCallParams } from './types';
5
+ import * as utilHttp from './utils/http';
5
6
  /**
6
7
  * 发起接口调用
7
8
  *
8
9
  * @param apiParam 接口调用参数 { api, version, token?, params?, files?, config?, host? }
9
10
  */
10
- export declare function call(apiParam: ApiCallParams): Promise<import("axios").AxiosResponse<any, any, {}>>;
11
+ export declare function call(apiParam: ApiCallParams): Promise<utilHttp.HttpResponse<unknown>>;
11
12
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAa7C;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,QAAQ,EAAE,aAAa,wDAgC3C"}
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,OAAO,KAAK,QAAQ,MAAM,cAAc,CAAC;AAWzC;;;;GAIG;AACH,wBAAgB,IAAI,CAAC,QAAQ,EAAE,aAAa,2CAgC3C"}
package/dist/esm/index.js CHANGED
@@ -39,35 +39,77 @@ function getUrlTextArea(api, version) {
39
39
 
40
40
  // src/utils/http.ts
41
41
  import fs from "fs";
42
- import axios from "axios";
43
- import FormData from "form-data";
44
- var httpClient = axios.create({
45
- baseURL: getBaseUrl(),
46
- headers: {
47
- "User-Agent": "YZY-Open-Client 1.0.0 - Node"
42
+ import path from "path";
43
+ var USER_AGENT = "YZY-Open-Client 1.0.0 - Node";
44
+
45
+ class HttpError extends Error {
46
+ response;
47
+ constructor(response) {
48
+ super(`Request failed with status code ${response.status}`);
49
+ this.name = "HttpError";
50
+ this.response = response;
48
51
  }
49
- });
52
+ }
53
+ function getRequestUrl(url) {
54
+ return new URL(url, getBaseUrl()).toString();
55
+ }
56
+ function headersToObject(headers) {
57
+ const result = {};
58
+ headers.forEach((value, key) => {
59
+ result[key] = value;
60
+ });
61
+ return result;
62
+ }
63
+ async function parseResponseBody(resp) {
64
+ const text = await resp.text();
65
+ if (text === "") {
66
+ return null;
67
+ }
68
+ const contentType = resp.headers.get("content-type");
69
+ if (contentType?.includes("application/json")) {
70
+ return JSON.parse(text);
71
+ }
72
+ return text;
73
+ }
74
+ async function toHttpResponse(resp) {
75
+ const response = {
76
+ data: await parseResponseBody(resp),
77
+ status: resp.status,
78
+ statusText: resp.statusText,
79
+ headers: headersToObject(resp.headers),
80
+ url: resp.url
81
+ };
82
+ if (!resp.ok) {
83
+ throw new HttpError(response);
84
+ }
85
+ return response;
86
+ }
50
87
  async function post(url, params) {
51
- const resp = await httpClient.post(url, params, {
52
- headers: { "Content-type": "application/json;charset=UTF-8" }
88
+ const resp = await fetch(getRequestUrl(url), {
89
+ method: "POST",
90
+ headers: {
91
+ "User-Agent": USER_AGENT,
92
+ "Content-type": "application/json;charset=UTF-8"
93
+ },
94
+ body: params === undefined ? undefined : JSON.stringify(params)
53
95
  });
54
- return resp;
96
+ return toHttpResponse(resp);
55
97
  }
56
98
  async function upload(url, files) {
57
99
  const form = new FormData;
58
- if (files instanceof Map) {
59
- files.forEach((filePath, key) => {
60
- form.append(key, fs.createReadStream(filePath));
61
- });
62
- } else {
63
- for (const [key, filePath] of Object.entries(files)) {
64
- form.append(key, fs.createReadStream(filePath));
65
- }
100
+ for (const [key, filePath] of files instanceof Map ? files : Object.entries(files)) {
101
+ const data = await fs.promises.readFile(filePath);
102
+ const blob = new Blob([new Uint8Array(data)]);
103
+ form.append(key, blob, path.basename(filePath));
66
104
  }
67
- const resp = await httpClient.post(url, form, {
68
- headers: form.getHeaders()
105
+ const resp = await fetch(getRequestUrl(url), {
106
+ method: "POST",
107
+ headers: {
108
+ "User-Agent": USER_AGENT
109
+ },
110
+ body: form
69
111
  });
70
- return resp;
112
+ return toHttpResponse(resp);
71
113
  }
72
114
 
73
115
  // src/client.ts
@@ -2,6 +2,7 @@
2
2
  * Token 管理
3
3
  */
4
4
  import type { TokenParams } from './types';
5
+ import * as utilHttp from './utils/http';
5
6
  /**
6
7
  * 获取 Token
7
8
  *
@@ -11,5 +12,5 @@ import type { TokenParams } from './types';
11
12
  * 工具型应用获取 Token: authorize_type = authorization_code, 必传 code redirect_uri
12
13
  * 工具型应用/自用型应用刷新 Token: authorize_type = refresh_token, 必传 refresh_token
13
14
  */
14
- export declare function get(params: TokenParams): Promise<import("axios").AxiosResponse<any, any, {}>>;
15
+ export declare function get(params: TokenParams): Promise<utilHttp.HttpResponse<unknown>>;
15
16
  //# sourceMappingURL=token.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/token.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAU3C;;;;;;;;GAQG;AACH,wBAAgB,GAAG,CAAC,MAAM,EAAE,WAAW,wDA4CtC"}
1
+ {"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/token.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,OAAO,KAAK,QAAQ,MAAM,cAAc,CAAC;AAQzC;;;;;;;;GAQG;AACH,wBAAgB,GAAG,CAAC,MAAM,EAAE,WAAW,2CA4CtC"}
@@ -1,18 +1,29 @@
1
1
  /**
2
2
  * HTTP 请求工具
3
3
  */
4
+ export interface HttpResponse<T = unknown> {
5
+ data: T;
6
+ status: number;
7
+ statusText: string;
8
+ headers: Record<string, string>;
9
+ url: string;
10
+ }
11
+ export declare class HttpError<T = unknown> extends Error {
12
+ response: HttpResponse<T>;
13
+ constructor(response: HttpResponse<T>);
14
+ }
4
15
  /**
5
16
  * 发起 POST 请求
6
17
  *
7
18
  * @param url 支持绝对路径、相对路径
8
19
  * @param params POST 参数
9
20
  */
10
- export declare function post(url: string, params?: Record<string, unknown>): Promise<import("axios").AxiosResponse<any, any, {}>>;
21
+ export declare function post(url: string, params?: Record<string, unknown>): Promise<HttpResponse<unknown>>;
11
22
  /**
12
23
  * 发起上传文件请求
13
24
  *
14
25
  * @param url 支持绝对路径、相对路径
15
26
  * @param files 上传文件参数,支持 Map 或 Object。示例: {"image": "/path/to/filename.jpg"}
16
27
  */
17
- export declare function upload(url: string, files: Map<string, string> | Record<string, string>): Promise<import("axios").AxiosResponse<any, any, {}>>;
28
+ export declare function upload(url: string, files: Map<string, string> | Record<string, string>): Promise<HttpResponse<unknown>>;
18
29
  //# sourceMappingURL=http.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/utils/http.ts"],"names":[],"mappings":"AAAA;;GAEG;AAeH;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,wDAKvE;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,wDAiB5F"}
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/utils/http.ts"],"names":[],"mappings":"AAAA;;GAEG;AAQH,MAAM,WAAW,YAAY,CAAC,CAAC,GAAG,OAAO;IACvC,IAAI,EAAE,CAAC,CAAC;IACR,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,GAAG,EAAE,MAAM,CAAC;CACb;AAED,qBAAa,SAAS,CAAC,CAAC,GAAG,OAAO,CAAE,SAAQ,KAAK;IAC/C,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC;gBAEd,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC;CAKtC;AA4CD;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,kCAUvE;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,kCAiB5F"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waywake/youzanyun-sdk",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "YouzanYun SDK for Node",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",
@@ -23,8 +23,8 @@
23
23
  ],
24
24
  "scripts": {
25
25
  "build": "bun run build:cjs && bun run build:esm",
26
- "build:cjs": "bun build src/index.ts --outdir dist/cjs --target node --format cjs --external axios --external form-data && tsc -p tsconfig.cjs.json --emitDeclarationOnly && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
27
- "build:esm": "bun build src/index.ts --outdir dist/esm --target node --format esm --external axios --external form-data && tsc -p tsconfig.esm.json --emitDeclarationOnly",
26
+ "build:cjs": "bun build src/index.ts --outdir dist/cjs --target node --format cjs && tsc -p tsconfig.cjs.json --emitDeclarationOnly && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
27
+ "build:esm": "bun build src/index.ts --outdir dist/esm --target node --format esm && tsc -p tsconfig.esm.json --emitDeclarationOnly",
28
28
  "clean": "rm -rf dist",
29
29
  "test": "bun test",
30
30
  "prepublishOnly": "bun run clean && bun run build"
@@ -46,11 +46,7 @@
46
46
  "url": "https://github.com/waywake/youzan-sdk-ts/issues"
47
47
  },
48
48
  "engines": {
49
- "node": ">=16.0.0"
50
- },
51
- "dependencies": {
52
- "axios": "^1.7.0",
53
- "form-data": "^4.0.0"
49
+ "node": ">=18.0.0"
54
50
  },
55
51
  "devDependencies": {
56
52
  "@types/bun": "^1.1.0",