@acedatacloud/sdk 0.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.
Files changed (47) hide show
  1. package/README.md +97 -0
  2. package/dist/client.d.ts +31 -0
  3. package/dist/client.js +46 -0
  4. package/dist/index.d.ts +4 -0
  5. package/dist/index.js +20 -0
  6. package/dist/resources/audio.d.ts +17 -0
  7. package/dist/resources/audio.js +30 -0
  8. package/dist/resources/chat.d.ts +30 -0
  9. package/dist/resources/chat.js +38 -0
  10. package/dist/resources/files.d.ts +9 -0
  11. package/dist/resources/files.js +59 -0
  12. package/dist/resources/images.d.ts +18 -0
  13. package/dist/resources/images.js +32 -0
  14. package/dist/resources/openai.d.ts +46 -0
  15. package/dist/resources/openai.js +59 -0
  16. package/dist/resources/platform.d.ts +41 -0
  17. package/dist/resources/platform.js +76 -0
  18. package/dist/resources/search.d.ts +14 -0
  19. package/dist/resources/search.js +22 -0
  20. package/dist/resources/tasks.d.ts +14 -0
  21. package/dist/resources/tasks.js +36 -0
  22. package/dist/resources/video.d.ts +17 -0
  23. package/dist/resources/video.js +30 -0
  24. package/dist/runtime/errors.d.ts +44 -0
  25. package/dist/runtime/errors.js +89 -0
  26. package/dist/runtime/index.d.ts +3 -0
  27. package/dist/runtime/index.js +19 -0
  28. package/dist/runtime/tasks.d.ts +17 -0
  29. package/dist/runtime/tasks.js +46 -0
  30. package/dist/runtime/transport.d.ts +31 -0
  31. package/dist/runtime/transport.js +210 -0
  32. package/package.json +28 -0
  33. package/src/client.ts +56 -0
  34. package/src/index.ts +19 -0
  35. package/src/resources/audio.ts +34 -0
  36. package/src/resources/chat.ts +63 -0
  37. package/src/resources/files.ts +27 -0
  38. package/src/resources/images.ts +36 -0
  39. package/src/resources/openai.ts +96 -0
  40. package/src/resources/platform.ts +77 -0
  41. package/src/resources/search.ts +23 -0
  42. package/src/resources/tasks.ts +38 -0
  43. package/src/resources/video.ts +34 -0
  44. package/src/runtime/errors.ts +93 -0
  45. package/src/runtime/index.ts +15 -0
  46. package/src/runtime/tasks.ts +57 -0
  47. package/src/runtime/transport.ts +257 -0
@@ -0,0 +1,14 @@
1
+ /** Search resources. */
2
+ import { Transport } from '../runtime/transport';
3
+ export declare class Search {
4
+ private transport;
5
+ constructor(transport: Transport);
6
+ google(opts: {
7
+ query: string;
8
+ type?: string;
9
+ country?: string;
10
+ language?: string;
11
+ page?: number;
12
+ [key: string]: unknown;
13
+ }): Promise<Record<string, unknown>>;
14
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ /** Search resources. */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Search = void 0;
5
+ class Search {
6
+ transport;
7
+ constructor(transport) {
8
+ this.transport = transport;
9
+ }
10
+ async google(opts) {
11
+ const { query, type = 'search', country, language, page, ...rest } = opts;
12
+ const body = { query, type, ...rest };
13
+ if (country !== undefined)
14
+ body.country = country;
15
+ if (language !== undefined)
16
+ body.language = language;
17
+ if (page !== undefined)
18
+ body.page = page;
19
+ return this.transport.request('POST', '/serp/google', { json: body });
20
+ }
21
+ }
22
+ exports.Search = Search;
@@ -0,0 +1,14 @@
1
+ /** Cross-service task retrieval. */
2
+ import { Transport } from '../runtime/transport';
3
+ export declare class Tasks {
4
+ private transport;
5
+ constructor(transport: Transport);
6
+ get(taskId: string, opts?: {
7
+ service?: string;
8
+ }): Promise<Record<string, unknown>>;
9
+ wait(taskId: string, opts?: {
10
+ service?: string;
11
+ pollInterval?: number;
12
+ maxWait?: number;
13
+ }): Promise<Record<string, unknown>>;
14
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ /** Cross-service task retrieval. */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Tasks = void 0;
5
+ const tasks_1 = require("../runtime/tasks");
6
+ const SERVICE_TASK_ENDPOINTS = {
7
+ suno: '/suno/tasks',
8
+ 'nano-banana': '/nano-banana/tasks',
9
+ seedream: '/seedream/tasks',
10
+ seedance: '/seedance/tasks',
11
+ sora: '/sora/tasks',
12
+ midjourney: '/midjourney/tasks',
13
+ luma: '/luma/tasks',
14
+ veo: '/veo/tasks',
15
+ flux: '/flux/tasks',
16
+ };
17
+ class Tasks {
18
+ transport;
19
+ constructor(transport) {
20
+ this.transport = transport;
21
+ }
22
+ async get(taskId, opts = {}) {
23
+ const service = opts.service ?? 'suno';
24
+ const endpoint = SERVICE_TASK_ENDPOINTS[service] ?? `/${service}/tasks`;
25
+ return this.transport.request('POST', endpoint, {
26
+ json: { id: taskId, action: 'retrieve' },
27
+ });
28
+ }
29
+ async wait(taskId, opts = {}) {
30
+ const service = opts.service ?? 'suno';
31
+ const endpoint = SERVICE_TASK_ENDPOINTS[service] ?? `/${service}/tasks`;
32
+ const handle = new tasks_1.TaskHandle(taskId, endpoint, this.transport);
33
+ return handle.wait({ pollInterval: opts.pollInterval, maxWait: opts.maxWait });
34
+ }
35
+ }
36
+ exports.Tasks = Tasks;
@@ -0,0 +1,17 @@
1
+ /** Video generation resources. */
2
+ import { Transport } from '../runtime/transport';
3
+ import { TaskHandle } from '../runtime/tasks';
4
+ export declare class Video {
5
+ private transport;
6
+ constructor(transport: Transport);
7
+ generate(opts: {
8
+ prompt: string;
9
+ model?: string;
10
+ imageUrl?: string;
11
+ callbackUrl?: string;
12
+ wait?: boolean;
13
+ pollInterval?: number;
14
+ maxWait?: number;
15
+ [key: string]: unknown;
16
+ }): Promise<Record<string, unknown> | TaskHandle>;
17
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ /** Video generation resources. */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Video = void 0;
5
+ const tasks_1 = require("../runtime/tasks");
6
+ class Video {
7
+ transport;
8
+ constructor(transport) {
9
+ this.transport = transport;
10
+ }
11
+ async generate(opts) {
12
+ const { prompt, model, imageUrl, callbackUrl, wait: shouldWait, pollInterval, maxWait, ...rest } = opts;
13
+ const body = { prompt, ...rest };
14
+ if (model !== undefined)
15
+ body.model = model;
16
+ if (imageUrl !== undefined)
17
+ body.image_url = imageUrl;
18
+ if (callbackUrl !== undefined)
19
+ body.callback_url = callbackUrl;
20
+ const result = await this.transport.request('POST', '/sora/videos', { json: body });
21
+ const taskId = result.task_id;
22
+ if (!taskId || (result.data && !shouldWait))
23
+ return result;
24
+ const handle = new tasks_1.TaskHandle(taskId, '/sora/tasks', this.transport);
25
+ if (shouldWait)
26
+ return handle.wait({ pollInterval, maxWait });
27
+ return handle;
28
+ }
29
+ }
30
+ exports.Video = Video;
@@ -0,0 +1,44 @@
1
+ /** AceDataCloud SDK errors. */
2
+ export declare class AceDataCloudError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ export declare class TransportError extends AceDataCloudError {
6
+ constructor(message: string);
7
+ }
8
+ export declare class APIError extends AceDataCloudError {
9
+ statusCode: number;
10
+ code: string;
11
+ traceId?: string;
12
+ body: Record<string, unknown>;
13
+ constructor(opts: {
14
+ message: string;
15
+ statusCode: number;
16
+ code: string;
17
+ traceId?: string;
18
+ body?: Record<string, unknown>;
19
+ });
20
+ }
21
+ export declare class AuthenticationError extends APIError {
22
+ constructor(opts: ConstructorParameters<typeof APIError>[0]);
23
+ }
24
+ export declare class TokenMismatchError extends APIError {
25
+ constructor(opts: ConstructorParameters<typeof APIError>[0]);
26
+ }
27
+ export declare class RateLimitError extends APIError {
28
+ constructor(opts: ConstructorParameters<typeof APIError>[0]);
29
+ }
30
+ export declare class ValidationError extends APIError {
31
+ constructor(opts: ConstructorParameters<typeof APIError>[0]);
32
+ }
33
+ export declare class InsufficientBalanceError extends APIError {
34
+ constructor(opts: ConstructorParameters<typeof APIError>[0]);
35
+ }
36
+ export declare class ResourceDisabledError extends APIError {
37
+ constructor(opts: ConstructorParameters<typeof APIError>[0]);
38
+ }
39
+ export declare class ModerationError extends APIError {
40
+ constructor(opts: ConstructorParameters<typeof APIError>[0]);
41
+ }
42
+ export declare class TimeoutError extends APIError {
43
+ constructor(opts: ConstructorParameters<typeof APIError>[0]);
44
+ }
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ /** AceDataCloud SDK errors. */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.TimeoutError = exports.ModerationError = exports.ResourceDisabledError = exports.InsufficientBalanceError = exports.ValidationError = exports.RateLimitError = exports.TokenMismatchError = exports.AuthenticationError = exports.APIError = exports.TransportError = exports.AceDataCloudError = void 0;
5
+ class AceDataCloudError extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ this.name = 'AceDataCloudError';
9
+ }
10
+ }
11
+ exports.AceDataCloudError = AceDataCloudError;
12
+ class TransportError extends AceDataCloudError {
13
+ constructor(message) {
14
+ super(message);
15
+ this.name = 'TransportError';
16
+ }
17
+ }
18
+ exports.TransportError = TransportError;
19
+ class APIError extends AceDataCloudError {
20
+ statusCode;
21
+ code;
22
+ traceId;
23
+ body;
24
+ constructor(opts) {
25
+ super(opts.message);
26
+ this.name = 'APIError';
27
+ this.statusCode = opts.statusCode;
28
+ this.code = opts.code;
29
+ this.traceId = opts.traceId;
30
+ this.body = opts.body ?? {};
31
+ }
32
+ }
33
+ exports.APIError = APIError;
34
+ class AuthenticationError extends APIError {
35
+ constructor(opts) {
36
+ super(opts);
37
+ this.name = 'AuthenticationError';
38
+ }
39
+ }
40
+ exports.AuthenticationError = AuthenticationError;
41
+ class TokenMismatchError extends APIError {
42
+ constructor(opts) {
43
+ super(opts);
44
+ this.name = 'TokenMismatchError';
45
+ }
46
+ }
47
+ exports.TokenMismatchError = TokenMismatchError;
48
+ class RateLimitError extends APIError {
49
+ constructor(opts) {
50
+ super(opts);
51
+ this.name = 'RateLimitError';
52
+ }
53
+ }
54
+ exports.RateLimitError = RateLimitError;
55
+ class ValidationError extends APIError {
56
+ constructor(opts) {
57
+ super(opts);
58
+ this.name = 'ValidationError';
59
+ }
60
+ }
61
+ exports.ValidationError = ValidationError;
62
+ class InsufficientBalanceError extends APIError {
63
+ constructor(opts) {
64
+ super(opts);
65
+ this.name = 'InsufficientBalanceError';
66
+ }
67
+ }
68
+ exports.InsufficientBalanceError = InsufficientBalanceError;
69
+ class ResourceDisabledError extends APIError {
70
+ constructor(opts) {
71
+ super(opts);
72
+ this.name = 'ResourceDisabledError';
73
+ }
74
+ }
75
+ exports.ResourceDisabledError = ResourceDisabledError;
76
+ class ModerationError extends APIError {
77
+ constructor(opts) {
78
+ super(opts);
79
+ this.name = 'ModerationError';
80
+ }
81
+ }
82
+ exports.ModerationError = ModerationError;
83
+ class TimeoutError extends APIError {
84
+ constructor(opts) {
85
+ super(opts);
86
+ this.name = 'TimeoutError';
87
+ }
88
+ }
89
+ exports.TimeoutError = TimeoutError;
@@ -0,0 +1,3 @@
1
+ export { Transport, TransportOptions } from './transport';
2
+ export { TaskHandle, TaskHandleOptions } from './tasks';
3
+ export { AceDataCloudError, APIError, AuthenticationError, InsufficientBalanceError, ModerationError, RateLimitError, ResourceDisabledError, TimeoutError, TokenMismatchError, TransportError, ValidationError, } from './errors';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ValidationError = exports.TransportError = exports.TokenMismatchError = exports.TimeoutError = exports.ResourceDisabledError = exports.RateLimitError = exports.ModerationError = exports.InsufficientBalanceError = exports.AuthenticationError = exports.APIError = exports.AceDataCloudError = exports.TaskHandle = exports.Transport = void 0;
4
+ var transport_1 = require("./transport");
5
+ Object.defineProperty(exports, "Transport", { enumerable: true, get: function () { return transport_1.Transport; } });
6
+ var tasks_1 = require("./tasks");
7
+ Object.defineProperty(exports, "TaskHandle", { enumerable: true, get: function () { return tasks_1.TaskHandle; } });
8
+ var errors_1 = require("./errors");
9
+ Object.defineProperty(exports, "AceDataCloudError", { enumerable: true, get: function () { return errors_1.AceDataCloudError; } });
10
+ Object.defineProperty(exports, "APIError", { enumerable: true, get: function () { return errors_1.APIError; } });
11
+ Object.defineProperty(exports, "AuthenticationError", { enumerable: true, get: function () { return errors_1.AuthenticationError; } });
12
+ Object.defineProperty(exports, "InsufficientBalanceError", { enumerable: true, get: function () { return errors_1.InsufficientBalanceError; } });
13
+ Object.defineProperty(exports, "ModerationError", { enumerable: true, get: function () { return errors_1.ModerationError; } });
14
+ Object.defineProperty(exports, "RateLimitError", { enumerable: true, get: function () { return errors_1.RateLimitError; } });
15
+ Object.defineProperty(exports, "ResourceDisabledError", { enumerable: true, get: function () { return errors_1.ResourceDisabledError; } });
16
+ Object.defineProperty(exports, "TimeoutError", { enumerable: true, get: function () { return errors_1.TimeoutError; } });
17
+ Object.defineProperty(exports, "TokenMismatchError", { enumerable: true, get: function () { return errors_1.TokenMismatchError; } });
18
+ Object.defineProperty(exports, "TransportError", { enumerable: true, get: function () { return errors_1.TransportError; } });
19
+ Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return errors_1.ValidationError; } });
@@ -0,0 +1,17 @@
1
+ /** Task polling abstraction. */
2
+ import { Transport } from './transport';
3
+ export interface TaskHandleOptions {
4
+ pollInterval?: number;
5
+ maxWait?: number;
6
+ }
7
+ export declare class TaskHandle {
8
+ readonly id: string;
9
+ private pollEndpoint;
10
+ private transport;
11
+ private _result;
12
+ constructor(taskId: string, pollEndpoint: string, transport: Transport);
13
+ get(): Promise<Record<string, unknown>>;
14
+ isCompleted(): Promise<boolean>;
15
+ wait(opts?: TaskHandleOptions): Promise<Record<string, unknown>>;
16
+ get result(): Record<string, unknown> | null;
17
+ }
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ /** Task polling abstraction. */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.TaskHandle = void 0;
5
+ class TaskHandle {
6
+ id;
7
+ pollEndpoint;
8
+ transport;
9
+ _result = null;
10
+ constructor(taskId, pollEndpoint, transport) {
11
+ this.id = taskId;
12
+ this.pollEndpoint = pollEndpoint;
13
+ this.transport = transport;
14
+ }
15
+ async get() {
16
+ return this.transport.request('POST', this.pollEndpoint, {
17
+ json: { id: this.id, action: 'retrieve' },
18
+ });
19
+ }
20
+ async isCompleted() {
21
+ const state = await this.get();
22
+ const response = (state.response ?? state);
23
+ const status = response.status;
24
+ return status === 'succeeded' || status === 'failed';
25
+ }
26
+ async wait(opts = {}) {
27
+ const pollInterval = opts.pollInterval ?? 3000;
28
+ const maxWait = opts.maxWait ?? 600_000;
29
+ const start = Date.now();
30
+ while (Date.now() - start < maxWait) {
31
+ const state = await this.get();
32
+ const response = (state.response ?? state);
33
+ const status = response.status;
34
+ if (status === 'succeeded' || status === 'failed') {
35
+ this._result = state;
36
+ return state;
37
+ }
38
+ await new Promise((resolve) => setTimeout(resolve, pollInterval));
39
+ }
40
+ throw new Error(`Task ${this.id} did not complete within ${maxWait}ms`);
41
+ }
42
+ get result() {
43
+ return this._result;
44
+ }
45
+ }
46
+ exports.TaskHandle = TaskHandle;
@@ -0,0 +1,31 @@
1
+ /** HTTP transport for AceDataCloud SDK. Uses native fetch (Node 18+). */
2
+ export interface TransportOptions {
3
+ apiToken?: string;
4
+ baseURL?: string;
5
+ platformBaseURL?: string;
6
+ timeout?: number;
7
+ maxRetries?: number;
8
+ headers?: Record<string, string>;
9
+ }
10
+ export declare class Transport {
11
+ private baseURL;
12
+ private platformBaseURL;
13
+ private timeout;
14
+ private maxRetries;
15
+ private headers;
16
+ constructor(opts?: TransportOptions);
17
+ request(method: string, path: string, opts?: {
18
+ json?: Record<string, unknown>;
19
+ params?: Record<string, string>;
20
+ platform?: boolean;
21
+ timeout?: number;
22
+ headers?: Record<string, string>;
23
+ }): Promise<Record<string, unknown>>;
24
+ requestStream(method: string, path: string, opts?: {
25
+ json?: Record<string, unknown>;
26
+ timeout?: number;
27
+ }): AsyncGenerator<string, void, unknown>;
28
+ upload(path: string, fileData: Buffer | Uint8Array, filename: string, opts?: {
29
+ timeout?: number;
30
+ }): Promise<Record<string, unknown>>;
31
+ }
@@ -0,0 +1,210 @@
1
+ "use strict";
2
+ /** HTTP transport for AceDataCloud SDK. Uses native fetch (Node 18+). */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.Transport = void 0;
5
+ const errors_1 = require("./errors");
6
+ const ERROR_CODE_MAP = {
7
+ invalid_token: errors_1.AuthenticationError,
8
+ token_expired: errors_1.AuthenticationError,
9
+ no_token: errors_1.AuthenticationError,
10
+ token_mismatched: errors_1.TokenMismatchError,
11
+ used_up: errors_1.InsufficientBalanceError,
12
+ disabled: errors_1.ResourceDisabledError,
13
+ too_many_requests: errors_1.RateLimitError,
14
+ bad_request: errors_1.ValidationError,
15
+ };
16
+ const RETRY_STATUS_CODES = new Set([408, 409, 429, 500, 502, 503, 504]);
17
+ function mapError(statusCode, body) {
18
+ const errorData = (body.error ?? {});
19
+ const code = (errorData.code ?? '');
20
+ const message = (errorData.message ?? '');
21
+ const traceId = body.trace_id;
22
+ let ErrorClass = ERROR_CODE_MAP[code];
23
+ if (!ErrorClass) {
24
+ if (statusCode === 403)
25
+ ErrorClass = errors_1.ModerationError;
26
+ else if (statusCode === 401)
27
+ ErrorClass = errors_1.AuthenticationError;
28
+ else if (statusCode === 429)
29
+ ErrorClass = errors_1.RateLimitError;
30
+ else if (statusCode === 400)
31
+ ErrorClass = errors_1.ValidationError;
32
+ else
33
+ ErrorClass = errors_1.APIError;
34
+ }
35
+ return new ErrorClass({ message, statusCode, code, traceId, body });
36
+ }
37
+ function backoffDelay(attempt) {
38
+ const base = Math.min(2 ** attempt, 8);
39
+ return base + Math.random() * 0.5;
40
+ }
41
+ function sleep(ms) {
42
+ return new Promise((resolve) => setTimeout(resolve, ms));
43
+ }
44
+ class Transport {
45
+ baseURL;
46
+ platformBaseURL;
47
+ timeout;
48
+ maxRetries;
49
+ headers;
50
+ constructor(opts = {}) {
51
+ const token = opts.apiToken ?? process.env.ACEDATACLOUD_API_TOKEN ?? '';
52
+ if (!token) {
53
+ throw new errors_1.AuthenticationError({
54
+ message: 'apiToken is required. Pass it to the client or set ACEDATACLOUD_API_TOKEN.',
55
+ statusCode: 0,
56
+ code: 'no_token',
57
+ });
58
+ }
59
+ this.baseURL = (opts.baseURL ?? 'https://api.acedata.cloud').replace(/\/+$/, '');
60
+ this.platformBaseURL = (opts.platformBaseURL ?? 'https://platform.acedata.cloud').replace(/\/+$/, '');
61
+ this.timeout = opts.timeout ?? 300_000;
62
+ this.maxRetries = opts.maxRetries ?? 2;
63
+ this.headers = {
64
+ accept: 'application/json',
65
+ authorization: `Bearer ${token}`,
66
+ 'content-type': 'application/json',
67
+ 'user-agent': 'acedatacloud-node/0.1.0',
68
+ ...(opts.headers ?? {}),
69
+ };
70
+ }
71
+ async request(method, path, opts = {}) {
72
+ const base = opts.platform ? this.platformBaseURL : this.baseURL;
73
+ let url = `${base}${path}`;
74
+ if (opts.params) {
75
+ const qs = new URLSearchParams(opts.params).toString();
76
+ url += `?${qs}`;
77
+ }
78
+ const headers = { ...this.headers, ...(opts.headers ?? {}) };
79
+ const timeoutMs = opts.timeout ?? this.timeout;
80
+ let lastError = null;
81
+ for (let attempt = 0; attempt <= this.maxRetries; attempt++) {
82
+ const controller = new AbortController();
83
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
84
+ try {
85
+ const resp = await fetch(url, {
86
+ method,
87
+ headers,
88
+ body: opts.json ? JSON.stringify(opts.json) : undefined,
89
+ signal: controller.signal,
90
+ });
91
+ clearTimeout(timer);
92
+ if (resp.status >= 400) {
93
+ let body;
94
+ try {
95
+ body = (await resp.json());
96
+ }
97
+ catch {
98
+ body = { error: { code: 'unknown', message: await resp.text() } };
99
+ }
100
+ if (RETRY_STATUS_CODES.has(resp.status) && attempt < this.maxRetries) {
101
+ await sleep(backoffDelay(attempt) * 1000);
102
+ continue;
103
+ }
104
+ throw mapError(resp.status, body);
105
+ }
106
+ return (await resp.json());
107
+ }
108
+ catch (err) {
109
+ clearTimeout(timer);
110
+ if (err instanceof errors_1.APIError)
111
+ throw err;
112
+ lastError = err;
113
+ if (attempt < this.maxRetries) {
114
+ await sleep(backoffDelay(attempt) * 1000);
115
+ continue;
116
+ }
117
+ }
118
+ }
119
+ throw lastError ?? new errors_1.TransportError('Request failed after retries');
120
+ }
121
+ async *requestStream(method, path, opts = {}) {
122
+ const url = `${this.baseURL}${path}`;
123
+ const headers = { ...this.headers, accept: 'text/event-stream' };
124
+ const controller = new AbortController();
125
+ const timer = setTimeout(() => controller.abort(), opts.timeout ?? this.timeout);
126
+ try {
127
+ const resp = await fetch(url, {
128
+ method,
129
+ headers,
130
+ body: opts.json ? JSON.stringify(opts.json) : undefined,
131
+ signal: controller.signal,
132
+ });
133
+ if (resp.status >= 400) {
134
+ let body;
135
+ try {
136
+ body = (await resp.json());
137
+ }
138
+ catch {
139
+ body = { error: { code: 'unknown', message: '' } };
140
+ }
141
+ throw mapError(resp.status, body);
142
+ }
143
+ if (!resp.body)
144
+ throw new errors_1.TransportError('No response body for stream');
145
+ const reader = resp.body.getReader();
146
+ const decoder = new TextDecoder();
147
+ let buffer = '';
148
+ while (true) {
149
+ const { done, value } = await reader.read();
150
+ if (done)
151
+ break;
152
+ buffer += decoder.decode(value, { stream: true });
153
+ const lines = buffer.split('\n');
154
+ buffer = lines.pop() ?? '';
155
+ for (const line of lines) {
156
+ if (line.startsWith('data: ')) {
157
+ const data = line.slice(6);
158
+ if (data === '[DONE]')
159
+ return;
160
+ yield data;
161
+ }
162
+ }
163
+ }
164
+ }
165
+ finally {
166
+ clearTimeout(timer);
167
+ }
168
+ }
169
+ async upload(path, fileData, filename, opts = {}) {
170
+ const url = `${this.platformBaseURL}${path}`;
171
+ const boundary = `----AceDataCloudBoundary${Date.now()}`;
172
+ const headers = {
173
+ ...this.headers,
174
+ 'content-type': `multipart/form-data; boundary=${boundary}`,
175
+ };
176
+ delete headers['content-type'];
177
+ const body = new FormData();
178
+ body.append('file', new Blob([fileData]), filename);
179
+ const authHeaders = {
180
+ authorization: this.headers.authorization,
181
+ 'user-agent': this.headers['user-agent'],
182
+ };
183
+ const controller = new AbortController();
184
+ const timer = setTimeout(() => controller.abort(), opts.timeout ?? this.timeout);
185
+ try {
186
+ const resp = await fetch(url, {
187
+ method: 'POST',
188
+ headers: authHeaders,
189
+ body,
190
+ signal: controller.signal,
191
+ });
192
+ clearTimeout(timer);
193
+ if (resp.status >= 400) {
194
+ let respBody;
195
+ try {
196
+ respBody = (await resp.json());
197
+ }
198
+ catch {
199
+ respBody = { error: { code: 'unknown', message: await resp.text() } };
200
+ }
201
+ throw mapError(resp.status, respBody);
202
+ }
203
+ return (await resp.json());
204
+ }
205
+ finally {
206
+ clearTimeout(timer);
207
+ }
208
+ }
209
+ }
210
+ exports.Transport = Transport;
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@acedatacloud/sdk",
3
+ "version": "0.0.1",
4
+ "description": "Official TypeScript SDK for AceDataCloud — AI-powered data services",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "license": "MIT",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "test": "jest --forceExit",
11
+ "lint": "tsc --noEmit"
12
+ },
13
+ "dependencies": {},
14
+ "devDependencies": {
15
+ "typescript": "^5.0.0",
16
+ "@types/node": "^20.0.0",
17
+ "jest": "^29.0.0",
18
+ "ts-jest": "^29.0.0",
19
+ "@types/jest": "^29.0.0"
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "src"
24
+ ],
25
+ "engines": {
26
+ "node": ">=18"
27
+ }
28
+ }