@codize/sdk 0.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.
@@ -0,0 +1,191 @@
1
+ import * as v from "valibot";
2
+
3
+ //#region src/client.d.ts
4
+ /**
5
+ * Internal `fetch`-compatible function signature used by the client.
6
+ */
7
+ type FetchFn = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
8
+ /**
9
+ * Options for creating a {@link CodizeClient} instance.
10
+ */
11
+ type CodizeClientOptions = {
12
+ /**
13
+ * API key issued by Codize.
14
+ */
15
+ apiKey: string;
16
+ /**
17
+ * Custom `fetch` implementation.
18
+ *
19
+ * Useful for tests, custom environments, or transport-level instrumentation.
20
+ */
21
+ fetchFn?: FetchFn;
22
+ };
23
+ /**
24
+ * Request payload for `sandbox.execute`.
25
+ */
26
+ type SandboxExecuteRequest = {
27
+ /**
28
+ * Language name used for execution.
29
+ */
30
+ language: "go" | "javascript" | "python" | "ruby" | "rust" | "typescript" | (string & {});
31
+ /**
32
+ * Source files to execute, with their content.
33
+ */
34
+ files: {
35
+ /**
36
+ * File name
37
+ */
38
+ name: string;
39
+ /**
40
+ * Full text content of the file.
41
+ */
42
+ content: string;
43
+ }[];
44
+ };
45
+ /**
46
+ * Response returned by `sandbox.execute`.
47
+ */
48
+ type SandboxExecuteResponse = {
49
+ /**
50
+ * Raw HTTP response headers.
51
+ */
52
+ headers: Headers;
53
+ data: {
54
+ /**
55
+ * Compile-stage output, if the selected language has a compile step.
56
+ */
57
+ compile: SandboxStageResult | null;
58
+ /**
59
+ * Run-stage output.
60
+ */
61
+ run: SandboxStageResult;
62
+ };
63
+ };
64
+ /**
65
+ * Output for a single sandbox stage (compile or run).
66
+ */
67
+ type SandboxStageResult = {
68
+ /**
69
+ * Standard output.
70
+ */
71
+ stdout: string;
72
+ /**
73
+ * Standard error.
74
+ */
75
+ stderr: string;
76
+ /**
77
+ * Combined output of stdout and stderr
78
+ */
79
+ output: string;
80
+ /**
81
+ * Process exit code.
82
+ */
83
+ exitCode: number | null;
84
+ };
85
+ /**
86
+ * API client for Codize.
87
+ */
88
+ declare class CodizeClient {
89
+ private readonly _apiKey;
90
+ private readonly _fetchFn;
91
+ private readonly _baseUrl;
92
+ /**
93
+ * Namespace for sandbox APIs.
94
+ */
95
+ readonly sandbox: {
96
+ /**
97
+ * Executes code in the Codize sandbox.
98
+ *
99
+ * @param request Execution request containing language and files.
100
+ * @returns Stage results and raw response headers.
101
+ */
102
+ execute: (request: SandboxExecuteRequest) => Promise<SandboxExecuteResponse>;
103
+ };
104
+ /**
105
+ * Creates a new client.
106
+ *
107
+ * @param options Client configuration.
108
+ */
109
+ constructor(options: CodizeClientOptions);
110
+ /**
111
+ * Sends an execution request to the sandbox endpoint.
112
+ *
113
+ * @param request Execution request payload.
114
+ * @returns Parsed sandbox execution response.
115
+ */
116
+ private _sandboxExecute;
117
+ /**
118
+ * Converts a failed HTTP response into a rich error object.
119
+ *
120
+ * @param response Failed API response.
121
+ * @returns Structured {@link CodizeApiError} when possible, otherwise `Error`.
122
+ */
123
+ private _apiError;
124
+ }
125
+ //#endregion
126
+ //#region src/error.d.ts
127
+ /**
128
+ * Schema for a single detailed validation error item returned by the API.
129
+ */
130
+ declare const apiErrorDetailsSchema: v.ObjectSchema<{
131
+ readonly message: v.StringSchema<undefined>;
132
+ readonly path: v.ArraySchema<v.UnionSchema<[v.StringSchema<undefined>, v.NumberSchema<undefined>], undefined>, undefined>;
133
+ }, undefined>;
134
+ /**
135
+ * Detailed validation error information returned by the API.
136
+ */
137
+ type ApiErrorDetails = v.InferOutput<typeof apiErrorDetailsSchema>;
138
+ /**
139
+ * API error code.
140
+ */
141
+ type ApiErrorCode = "UNAUTHORIZED" | "INTERNAL_ERROR" | "RATE_LIMITED" | "NOT_FOUND" | "VALIDATION_ERROR" | (string & {});
142
+ /**
143
+ * API error response.
144
+ */
145
+ type ApiErrorResponse = {
146
+ error: {
147
+ /**
148
+ * Machine-readable error code.
149
+ */
150
+ code: ApiErrorCode;
151
+ /**
152
+ * Human-readable error message.
153
+ */
154
+ message: string;
155
+ /**
156
+ * Optional field-level validation errors.
157
+ */
158
+ errors?: ApiErrorDetails[];
159
+ };
160
+ };
161
+ /**
162
+ * Error thrown for non-2xx API responses with a structured error body.
163
+ */
164
+ declare class CodizeApiError extends Error {
165
+ /**
166
+ * Machine-readable API error code.
167
+ */
168
+ readonly code: ApiErrorResponse["error"]["code"];
169
+ /**
170
+ * Optional field-level validation errors.
171
+ */
172
+ readonly errors?: ApiErrorResponse["error"]["errors"];
173
+ /**
174
+ * HTTP status code of the response.
175
+ */
176
+ readonly status: number;
177
+ /**
178
+ * Raw response headers.
179
+ */
180
+ readonly headers: Headers;
181
+ /**
182
+ * Creates a rich API error object.
183
+ *
184
+ * @param status HTTP status code.
185
+ * @param headers Raw response headers.
186
+ * @param response Parsed API error payload.
187
+ */
188
+ constructor(status: number, headers: Headers, response: ApiErrorResponse);
189
+ }
190
+ //#endregion
191
+ export { type ApiErrorCode, type ApiErrorDetails, type ApiErrorResponse, CodizeApiError, CodizeClient, type CodizeClientOptions, type SandboxExecuteRequest, type SandboxExecuteResponse, type SandboxStageResult };
package/dist/index.mjs ADDED
@@ -0,0 +1,126 @@
1
+ import * as v from "valibot";
2
+
3
+ //#region src/error.ts
4
+ /**
5
+ * Schema for a single detailed validation error item returned by the API.
6
+ */
7
+ const apiErrorDetailsSchema = v.object({
8
+ message: v.string(),
9
+ path: v.array(v.union([v.string(), v.number()]))
10
+ });
11
+ /**
12
+ * Schema for the top-level API error response payload.
13
+ */
14
+ const apiErrorResponseSchema = v.object({ error: v.object({
15
+ code: v.string(),
16
+ message: v.string(),
17
+ errors: v.optional(v.array(apiErrorDetailsSchema))
18
+ }) });
19
+ /**
20
+ * Error thrown for non-2xx API responses with a structured error body.
21
+ */
22
+ var CodizeApiError = class extends Error {
23
+ /**
24
+ * Machine-readable API error code.
25
+ */
26
+ code;
27
+ /**
28
+ * Optional field-level validation errors.
29
+ */
30
+ errors;
31
+ /**
32
+ * HTTP status code of the response.
33
+ */
34
+ status;
35
+ /**
36
+ * Raw response headers.
37
+ */
38
+ headers;
39
+ /**
40
+ * Creates a rich API error object.
41
+ *
42
+ * @param status HTTP status code.
43
+ * @param headers Raw response headers.
44
+ * @param response Parsed API error payload.
45
+ */
46
+ constructor(status, headers, response) {
47
+ super(response.error.message);
48
+ this.name = "CodizeApiError";
49
+ this.code = response.error.code;
50
+ this.message = response.error.message;
51
+ this.errors = response.error.errors;
52
+ this.status = status;
53
+ this.headers = headers;
54
+ }
55
+ };
56
+
57
+ //#endregion
58
+ //#region src/client.ts
59
+ /**
60
+ * API client for Codize.
61
+ */
62
+ var CodizeClient = class {
63
+ _apiKey;
64
+ _fetchFn;
65
+ _baseUrl = "https://codize.dev";
66
+ /**
67
+ * Namespace for sandbox APIs.
68
+ */
69
+ sandbox;
70
+ /**
71
+ * Creates a new client.
72
+ *
73
+ * @param options Client configuration.
74
+ */
75
+ constructor(options) {
76
+ this._apiKey = options.apiKey;
77
+ this._fetchFn = options.fetchFn ?? fetch.bind(globalThis);
78
+ this.sandbox = { execute: this._sandboxExecute.bind(this) };
79
+ }
80
+ /**
81
+ * Sends an execution request to the sandbox endpoint.
82
+ *
83
+ * @param request Execution request payload.
84
+ * @returns Parsed sandbox execution response.
85
+ */
86
+ async _sandboxExecute(request) {
87
+ const response = await this._fetchFn(new URL("/api/v1/sandbox/execute", this._baseUrl), {
88
+ method: "POST",
89
+ headers: {
90
+ "Content-Type": "application/json",
91
+ Authorization: `Bearer ${this._apiKey}`
92
+ },
93
+ body: JSON.stringify(request)
94
+ });
95
+ if (!response.ok) throw await this._apiError(response);
96
+ const data = await response.json();
97
+ return {
98
+ headers: response.headers,
99
+ data: {
100
+ compile: data.compile,
101
+ run: data.run
102
+ }
103
+ };
104
+ }
105
+ /**
106
+ * Converts a failed HTTP response into a rich error object.
107
+ *
108
+ * @param response Failed API response.
109
+ * @returns Structured {@link CodizeApiError} when possible, otherwise `Error`.
110
+ */
111
+ async _apiError(response) {
112
+ const errorData = await response.text();
113
+ let parsedJson;
114
+ try {
115
+ parsedJson = JSON.parse(errorData);
116
+ } catch {
117
+ return /* @__PURE__ */ new Error(`Unexpected API error: ${response.status} ${response.statusText} - ${errorData}`);
118
+ }
119
+ const parsedError = v.safeParse(apiErrorResponseSchema, parsedJson);
120
+ if (parsedError.success) return new CodizeApiError(response.status, response.headers, parsedError.output);
121
+ else return /* @__PURE__ */ new Error(`Unexpected API error: ${response.status} ${response.statusText} - ${errorData}`);
122
+ }
123
+ };
124
+
125
+ //#endregion
126
+ export { CodizeApiError, CodizeClient };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@codize/sdk",
3
+ "version": "0.0.0",
4
+ "description": "Official TypeScript SDK for the Codize API",
5
+ "homepage": "https://github.com/codize-dev/sdk",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/codize-dev/sdk.git",
9
+ "directory": "typescript"
10
+ },
11
+ "license": "MIT",
12
+ "author": "koki-develop <kou.pg.0131@gmail.com>",
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.mts",
17
+ "default": "./dist/index.mjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "scripts": {
24
+ "build": "tsdown",
25
+ "prepublishOnly": "bun run build"
26
+ },
27
+ "devDependencies": {
28
+ "@types/bun": "1.3.9",
29
+ "tsdown": "0.21.0-beta.2",
30
+ "typescript": "5.9.3"
31
+ },
32
+ "dependencies": {
33
+ "valibot": "1.2.0"
34
+ }
35
+ }