@kirimdev/sdk 1.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.
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/dist/client.d.ts +52 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +75 -0
- package/dist/client.js.map +1 -0
- package/dist/errors.d.ts +70 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +167 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +71 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +197 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/pagination.d.ts +36 -0
- package/dist/pagination.d.ts.map +1 -0
- package/dist/pagination.js +59 -0
- package/dist/pagination.js.map +1 -0
- package/dist/resources/contacts.d.ts +36 -0
- package/dist/resources/contacts.d.ts.map +1 -0
- package/dist/resources/contacts.js +68 -0
- package/dist/resources/contacts.js.map +1 -0
- package/dist/resources/conversations.d.ts +23 -0
- package/dist/resources/conversations.d.ts.map +1 -0
- package/dist/resources/conversations.js +43 -0
- package/dist/resources/conversations.js.map +1 -0
- package/dist/resources/labels.d.ts +17 -0
- package/dist/resources/labels.d.ts.map +1 -0
- package/dist/resources/labels.js +41 -0
- package/dist/resources/labels.js.map +1 -0
- package/dist/resources/messages.d.ts +19 -0
- package/dist/resources/messages.d.ts.map +1 -0
- package/dist/resources/messages.js +40 -0
- package/dist/resources/messages.js.map +1 -0
- package/dist/resources/templates.d.ts +10 -0
- package/dist/resources/templates.d.ts.map +1 -0
- package/dist/resources/templates.js +19 -0
- package/dist/resources/templates.js.map +1 -0
- package/dist/resources/webhook-deliveries.d.ts +19 -0
- package/dist/resources/webhook-deliveries.d.ts.map +1 -0
- package/dist/resources/webhook-deliveries.js +35 -0
- package/dist/resources/webhook-deliveries.js.map +1 -0
- package/dist/resources/webhook-subscriptions.d.ts +26 -0
- package/dist/resources/webhook-subscriptions.d.ts.map +1 -0
- package/dist/resources/webhook-subscriptions.js +60 -0
- package/dist/resources/webhook-subscriptions.js.map +1 -0
- package/dist/types.d.ts +55 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/webhooks.d.ts +100 -0
- package/dist/webhooks.d.ts.map +1 -0
- package/dist/webhooks.js +65 -0
- package/dist/webhooks.js.map +1 -0
- package/openapi.json +6181 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kirim
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# @kirimdev/sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript SDK for the [Kirim](https://kirim.chat) Public API.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @kirimdev/sdk
|
|
9
|
+
# or
|
|
10
|
+
bun add @kirimdev/sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quickstart
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { Kirim } from '@kirimdev/sdk'
|
|
17
|
+
|
|
18
|
+
const kirim = new Kirim({ apiKey: process.env.KIRIM_API_KEY! })
|
|
19
|
+
|
|
20
|
+
// Send a message
|
|
21
|
+
const msg = await kirim.messages.send({
|
|
22
|
+
to: '628123456789',
|
|
23
|
+
type: 'text',
|
|
24
|
+
text: { body: 'Halo dari SDK!' },
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
// Paginate
|
|
28
|
+
for await (const m of kirim.messages.list({ limit: 50 })) {
|
|
29
|
+
console.log(m.id, m.status)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Webhook verification
|
|
33
|
+
import { verifyWebhookSignature } from '@kirimdev/sdk/webhooks'
|
|
34
|
+
|
|
35
|
+
const ok = verifyWebhookSignature(rawBody, req.headers['x-kirim-signature'], secret)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Features
|
|
39
|
+
|
|
40
|
+
- Full coverage of the Kirim `/v1` API (~35 endpoints)
|
|
41
|
+
- Type-safe — generated from the live OpenAPI 3.1 spec
|
|
42
|
+
- Automatic retries with exponential backoff (429, 5xx, network errors)
|
|
43
|
+
- Automatic `Idempotency-Key` injection for POST requests
|
|
44
|
+
- Async iterator pagination (`for await ... of kirim.messages.list(...)`)
|
|
45
|
+
- Typed error class hierarchy keyed off stable API error codes
|
|
46
|
+
- Webhook HMAC-SHA256 verifier
|
|
47
|
+
- Zero Node-specific dependencies — works in Node 18+, Bun, Deno
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
new Kirim({
|
|
53
|
+
apiKey: 'kdv_live_...', // required
|
|
54
|
+
baseUrl: 'https://api-kckit.kirim.chat/v1', // optional
|
|
55
|
+
timeout: 30_000, // ms, default 30s
|
|
56
|
+
maxRetries: 2, // default 2
|
|
57
|
+
fetch: globalThis.fetch, // injectable for testing
|
|
58
|
+
})
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Top-level `Kirim` client. Owns one `HttpClient` and exposes resource
|
|
3
|
+
* namespaces. Construction is cheap; one instance per API key per process.
|
|
4
|
+
*/
|
|
5
|
+
import { type RequestOptions } from './http.js';
|
|
6
|
+
import { ContactsResource } from './resources/contacts.js';
|
|
7
|
+
import { ConversationsResource } from './resources/conversations.js';
|
|
8
|
+
import { LabelsResource } from './resources/labels.js';
|
|
9
|
+
import { MessagesResource } from './resources/messages.js';
|
|
10
|
+
import { TemplatesResource } from './resources/templates.js';
|
|
11
|
+
import { WebhookDeliveriesResource } from './resources/webhook-deliveries.js';
|
|
12
|
+
import { WebhookSubscriptionsResource } from './resources/webhook-subscriptions.js';
|
|
13
|
+
import type { MeContext } from './types.js';
|
|
14
|
+
export declare const SDK_VERSION = "1.0.0";
|
|
15
|
+
export interface KirimOptions {
|
|
16
|
+
/** Bearer API key. Format: `kdv_live_<...>`. Required. */
|
|
17
|
+
apiKey: string;
|
|
18
|
+
/** Override the API base URL (incl. `/v1`). Default: production. */
|
|
19
|
+
baseUrl?: string;
|
|
20
|
+
/** Per-request timeout in milliseconds. Default: 30_000. */
|
|
21
|
+
timeout?: number;
|
|
22
|
+
/** Retries on 429/5xx/network. Default: 2. */
|
|
23
|
+
maxRetries?: number;
|
|
24
|
+
/** Injectable fetch (for testing or custom transports). */
|
|
25
|
+
fetch?: typeof fetch;
|
|
26
|
+
/** Extra suffix appended to the SDK User-Agent (e.g. your app name). */
|
|
27
|
+
userAgentSuffix?: string;
|
|
28
|
+
}
|
|
29
|
+
export declare class Kirim {
|
|
30
|
+
readonly messages: MessagesResource;
|
|
31
|
+
readonly templates: TemplatesResource;
|
|
32
|
+
readonly conversations: ConversationsResource;
|
|
33
|
+
readonly contacts: ContactsResource;
|
|
34
|
+
readonly labels: LabelsResource;
|
|
35
|
+
readonly webhookSubscriptions: WebhookSubscriptionsResource;
|
|
36
|
+
readonly webhookDeliveries: WebhookDeliveriesResource;
|
|
37
|
+
private readonly http;
|
|
38
|
+
constructor(options: KirimOptions);
|
|
39
|
+
/** GET /me — introspect the calling API key. */
|
|
40
|
+
me(options?: RequestOptions): Promise<MeContext>;
|
|
41
|
+
/**
|
|
42
|
+
* Escape hatch for endpoints not yet wrapped by the SDK. Returns the raw
|
|
43
|
+
* decoded JSON body (envelope NOT unwrapped). Useful for previewing new
|
|
44
|
+
* API endpoints before the next SDK release.
|
|
45
|
+
*/
|
|
46
|
+
request<T = unknown>(method: 'GET' | 'POST' | 'PATCH' | 'DELETE', path: string, init?: {
|
|
47
|
+
body?: unknown;
|
|
48
|
+
query?: Record<string, string | number | boolean | undefined | null>;
|
|
49
|
+
options?: RequestOptions;
|
|
50
|
+
}): Promise<T>;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAiC,KAAK,cAAc,EAAE,MAAM,WAAW,CAAA;AAC9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAA;AAC1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EA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OAAO,EACjB,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,EAC3C,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE;QACL,IAAI,CAAC,EAAE,OAAO,CAAA;QACd,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,IAAI,CAAC,CAAA;QACpE,OAAO,CAAC,EAAE,cAAc,CAAA;KACzB,GACA,OAAO,CAAC,CAAC,CAAC;CASd"}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Top-level `Kirim` client. Owns one `HttpClient` and exposes resource
|
|
3
|
+
* namespaces. Construction is cheap; one instance per API key per process.
|
|
4
|
+
*/
|
|
5
|
+
import { HttpClient } from './http.js';
|
|
6
|
+
import { ContactsResource } from './resources/contacts.js';
|
|
7
|
+
import { ConversationsResource } from './resources/conversations.js';
|
|
8
|
+
import { LabelsResource } from './resources/labels.js';
|
|
9
|
+
import { MessagesResource } from './resources/messages.js';
|
|
10
|
+
import { TemplatesResource } from './resources/templates.js';
|
|
11
|
+
import { WebhookDeliveriesResource } from './resources/webhook-deliveries.js';
|
|
12
|
+
import { WebhookSubscriptionsResource } from './resources/webhook-subscriptions.js';
|
|
13
|
+
export const SDK_VERSION = '1.0.0';
|
|
14
|
+
const DEFAULT_BASE_URL = 'https://api-kckit.kirim.chat/v1';
|
|
15
|
+
const DEFAULT_TIMEOUT_MS = 30_000;
|
|
16
|
+
const DEFAULT_MAX_RETRIES = 2;
|
|
17
|
+
export class Kirim {
|
|
18
|
+
messages;
|
|
19
|
+
templates;
|
|
20
|
+
conversations;
|
|
21
|
+
contacts;
|
|
22
|
+
labels;
|
|
23
|
+
webhookSubscriptions;
|
|
24
|
+
webhookDeliveries;
|
|
25
|
+
http;
|
|
26
|
+
constructor(options) {
|
|
27
|
+
if (!options.apiKey) {
|
|
28
|
+
throw new TypeError('Kirim: `apiKey` is required.');
|
|
29
|
+
}
|
|
30
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
31
|
+
if (typeof fetchImpl !== 'function') {
|
|
32
|
+
throw new TypeError('Kirim: global `fetch` is not available. Pass `fetch` in options or upgrade to Node 18+.');
|
|
33
|
+
}
|
|
34
|
+
const ua = `kirimdev-sdk-js/${SDK_VERSION}${options.userAgentSuffix ? ` ${options.userAgentSuffix}` : ''}`;
|
|
35
|
+
const config = {
|
|
36
|
+
apiKey: options.apiKey,
|
|
37
|
+
baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
|
|
38
|
+
timeout: options.timeout ?? DEFAULT_TIMEOUT_MS,
|
|
39
|
+
maxRetries: options.maxRetries ?? DEFAULT_MAX_RETRIES,
|
|
40
|
+
fetch: fetchImpl,
|
|
41
|
+
userAgent: ua,
|
|
42
|
+
};
|
|
43
|
+
this.http = new HttpClient(config);
|
|
44
|
+
this.messages = new MessagesResource(this.http);
|
|
45
|
+
this.templates = new TemplatesResource(this.http);
|
|
46
|
+
this.conversations = new ConversationsResource(this.http);
|
|
47
|
+
this.contacts = new ContactsResource(this.http);
|
|
48
|
+
this.labels = new LabelsResource(this.http);
|
|
49
|
+
this.webhookSubscriptions = new WebhookSubscriptionsResource(this.http);
|
|
50
|
+
this.webhookDeliveries = new WebhookDeliveriesResource(this.http);
|
|
51
|
+
}
|
|
52
|
+
/** GET /me — introspect the calling API key. */
|
|
53
|
+
me(options) {
|
|
54
|
+
return this.http.requestData({
|
|
55
|
+
method: 'GET',
|
|
56
|
+
path: '/me',
|
|
57
|
+
...(options ? { options } : {}),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Escape hatch for endpoints not yet wrapped by the SDK. Returns the raw
|
|
62
|
+
* decoded JSON body (envelope NOT unwrapped). Useful for previewing new
|
|
63
|
+
* API endpoints before the next SDK release.
|
|
64
|
+
*/
|
|
65
|
+
request(method, path, init) {
|
|
66
|
+
return this.http.requestRaw({
|
|
67
|
+
method,
|
|
68
|
+
path,
|
|
69
|
+
...(init?.body !== undefined ? { body: init.body } : {}),
|
|
70
|
+
...(init?.query ? { query: init.query } : {}),
|
|
71
|
+
...(init?.options ? { options: init.options } : {}),
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,UAAU,EAA0C,MAAM,WAAW,CAAA;AAC9E,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAA;AAC1D,OAAO,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAA;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAA;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,yBAAyB,EAAE,MAAM,mCAAmC,CAAA;AAC7E,OAAO,EAAE,4BAA4B,EAAE,MAAM,sCAAsC,CAAA;AAGnF,MAAM,CAAC,MAAM,WAAW,GAAG,OAAO,CAAA;AAClC,MAAM,gBAAgB,GAAG,iCAAiC,CAAA;AAC1D,MAAM,kBAAkB,GAAG,MAAM,CAAA;AACjC,MAAM,mBAAmB,GAAG,CAAC,CAAA;AAiB7B,MAAM,OAAO,KAAK;IACP,QAAQ,CAAkB;IAC1B,SAAS,CAAmB;IAC5B,aAAa,CAAuB;IACpC,QAAQ,CAAkB;IAC1B,MAAM,CAAgB;IACtB,oBAAoB,CAA8B;IAClD,iBAAiB,CAA2B;IAEpC,IAAI,CAAY;IAEjC,YAAY,OAAqB;QAC/B,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YACpB,MAAM,IAAI,SAAS,CAAC,8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AyB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACnE,CAAC;IAED,gDAAgD;IAChD,EAAE,CAAC,OAAwB;QACzB,OAAO,IAAI,CAAC,IAAI,CAAC,WAAW,CAAY;YACtC,MAAM,EAAE,KAAK;YACb,IAAI,EAAE,KAAK;YACX,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAChC,CAAC,CAAA;IACJ,CAAC;IAED;;;;OAIG;IACH,OAAO,CACL,MAA2C,EAC3C,IAAY,EACZ,IAIC;QAED,OAAO,IAAI,CAAC,IAAI,CAAC,UAAU,CAAI;YAC7B,MAAM;YACN,IAAI;YACJ,GAAG,CAAC,IAAI,EAAE,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACxD,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7C,GAAG,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACpD,CAAC,CAAA;IACJ,CAAC;CACF"}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error class hierarchy for `@kirimdev/sdk`.
|
|
3
|
+
*
|
|
4
|
+
* Every HTTP error from the Kirim API is mapped to a subclass of
|
|
5
|
+
* `KirimError`. Network/timeout failures throw `ConnectionError`. The
|
|
6
|
+
* subclass tree mirrors `error.type` from the API envelope so callers can
|
|
7
|
+
* use `instanceof` for coarse categorization and inspect `code` for the
|
|
8
|
+
* specific stable identifier.
|
|
9
|
+
*
|
|
10
|
+
* Source of truth for codes: `apps/api/src/lib/api-error.ts` (ERROR_CATALOG).
|
|
11
|
+
*/
|
|
12
|
+
export type KirimErrorType = 'invalid_request_error' | 'authentication_error' | 'permission_error' | 'not_found' | 'conflict' | 'rate_limit_error' | 'api_error' | 'connection_error';
|
|
13
|
+
export interface KirimErrorPayload {
|
|
14
|
+
type: KirimErrorType;
|
|
15
|
+
code: string;
|
|
16
|
+
message: string;
|
|
17
|
+
status: number;
|
|
18
|
+
requestId: string | null;
|
|
19
|
+
param?: string | undefined;
|
|
20
|
+
/** Raw decoded error body, for debugging. */
|
|
21
|
+
raw?: unknown;
|
|
22
|
+
}
|
|
23
|
+
/** Base class. All errors thrown by the SDK extend this. */
|
|
24
|
+
export declare class KirimError extends Error {
|
|
25
|
+
readonly type: KirimErrorType;
|
|
26
|
+
readonly code: string;
|
|
27
|
+
readonly status: number;
|
|
28
|
+
readonly requestId: string | null;
|
|
29
|
+
readonly param: string | undefined;
|
|
30
|
+
readonly raw: unknown;
|
|
31
|
+
constructor(payload: KirimErrorPayload);
|
|
32
|
+
}
|
|
33
|
+
export declare class InvalidRequestError extends KirimError {
|
|
34
|
+
constructor(payload: KirimErrorPayload);
|
|
35
|
+
}
|
|
36
|
+
export declare class AuthenticationError extends KirimError {
|
|
37
|
+
constructor(payload: KirimErrorPayload);
|
|
38
|
+
}
|
|
39
|
+
export declare class PermissionError extends KirimError {
|
|
40
|
+
constructor(payload: KirimErrorPayload);
|
|
41
|
+
}
|
|
42
|
+
export declare class NotFoundError extends KirimError {
|
|
43
|
+
constructor(payload: KirimErrorPayload);
|
|
44
|
+
}
|
|
45
|
+
export declare class ConflictError extends KirimError {
|
|
46
|
+
constructor(payload: KirimErrorPayload);
|
|
47
|
+
}
|
|
48
|
+
export declare class RateLimitError extends KirimError {
|
|
49
|
+
/** Seconds the server asked us to wait, if `Retry-After` was a delta. */
|
|
50
|
+
readonly retryAfter: number | null;
|
|
51
|
+
constructor(payload: KirimErrorPayload & {
|
|
52
|
+
retryAfter: number | null;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
export declare class ApiServerError extends KirimError {
|
|
56
|
+
constructor(payload: KirimErrorPayload);
|
|
57
|
+
}
|
|
58
|
+
/** No response received (DNS, TCP, abort, timeout, CORS). */
|
|
59
|
+
export declare class ConnectionError extends KirimError {
|
|
60
|
+
readonly cause: unknown;
|
|
61
|
+
constructor(message: string, cause: unknown);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Build a typed error from an HTTP response body + status. The body is
|
|
65
|
+
* expected to follow `ErrorResponse` (see `apps/api/src/lib/api-response.ts`)
|
|
66
|
+
* but we tolerate non-conforming bodies (e.g. a stray HTML 502 from a CDN)
|
|
67
|
+
* by falling back to a generic message.
|
|
68
|
+
*/
|
|
69
|
+
export declare function fromHttpResponse(status: number, body: unknown, requestId: string | null, retryAfterSeconds: number | null): KirimError;
|
|
70
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,MAAM,MAAM,cAAc,GACtB,uBAAuB,GACvB,sBAAsB,GACtB,kBAAkB,GAClB,WAAW,GACX,UAAU,GACV,kBAAkB,GAClB,WAAW,GACX,kBAAkB,CAAA;AAEtB,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,cAAc,CAAA;IACpB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;IAC1B,6CAA6C;IAC7C,GAAG,CAAC,EAAE,OAAO,CAAA;CACd;AAED,4DAA4D;AAC5D,qBAAa,UAAW,SAAQ,KAAK;IACnC,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAA;IAC7B,QAAQ,CAA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|
1
|
+
/**
|
|
2
|
+
* Error class hierarchy for `@kirimdev/sdk`.
|
|
3
|
+
*
|
|
4
|
+
* Every HTTP error from the Kirim API is mapped to a subclass of
|
|
5
|
+
* `KirimError`. Network/timeout failures throw `ConnectionError`. The
|
|
6
|
+
* subclass tree mirrors `error.type` from the API envelope so callers can
|
|
7
|
+
* use `instanceof` for coarse categorization and inspect `code` for the
|
|
8
|
+
* specific stable identifier.
|
|
9
|
+
*
|
|
10
|
+
* Source of truth for codes: `apps/api/src/lib/api-error.ts` (ERROR_CATALOG).
|
|
11
|
+
*/
|
|
12
|
+
/** Base class. All errors thrown by the SDK extend this. */
|
|
13
|
+
export class KirimError extends Error {
|
|
14
|
+
type;
|
|
15
|
+
code;
|
|
16
|
+
status;
|
|
17
|
+
requestId;
|
|
18
|
+
param;
|
|
19
|
+
raw;
|
|
20
|
+
constructor(payload) {
|
|
21
|
+
super(payload.message);
|
|
22
|
+
this.name = 'KirimError';
|
|
23
|
+
this.type = payload.type;
|
|
24
|
+
this.code = payload.code;
|
|
25
|
+
this.status = payload.status;
|
|
26
|
+
this.requestId = payload.requestId;
|
|
27
|
+
this.param = payload.param;
|
|
28
|
+
this.raw = payload.raw;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class InvalidRequestError extends KirimError {
|
|
32
|
+
constructor(payload) {
|
|
33
|
+
super(payload);
|
|
34
|
+
this.name = 'InvalidRequestError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export class AuthenticationError extends KirimError {
|
|
38
|
+
constructor(payload) {
|
|
39
|
+
super(payload);
|
|
40
|
+
this.name = 'AuthenticationError';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export class PermissionError extends KirimError {
|
|
44
|
+
constructor(payload) {
|
|
45
|
+
super(payload);
|
|
46
|
+
this.name = 'PermissionError';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export class NotFoundError extends KirimError {
|
|
50
|
+
constructor(payload) {
|
|
51
|
+
super(payload);
|
|
52
|
+
this.name = 'NotFoundError';
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export class ConflictError extends KirimError {
|
|
56
|
+
constructor(payload) {
|
|
57
|
+
super(payload);
|
|
58
|
+
this.name = 'ConflictError';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export class RateLimitError extends KirimError {
|
|
62
|
+
/** Seconds the server asked us to wait, if `Retry-After` was a delta. */
|
|
63
|
+
retryAfter;
|
|
64
|
+
constructor(payload) {
|
|
65
|
+
super(payload);
|
|
66
|
+
this.name = 'RateLimitError';
|
|
67
|
+
this.retryAfter = payload.retryAfter;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export class ApiServerError extends KirimError {
|
|
71
|
+
constructor(payload) {
|
|
72
|
+
super(payload);
|
|
73
|
+
this.name = 'ApiServerError';
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** No response received (DNS, TCP, abort, timeout, CORS). */
|
|
77
|
+
export class ConnectionError extends KirimError {
|
|
78
|
+
cause;
|
|
79
|
+
constructor(message, cause) {
|
|
80
|
+
super({
|
|
81
|
+
type: 'connection_error',
|
|
82
|
+
code: 'connection_error',
|
|
83
|
+
message,
|
|
84
|
+
status: 0,
|
|
85
|
+
requestId: null,
|
|
86
|
+
});
|
|
87
|
+
this.name = 'ConnectionError';
|
|
88
|
+
this.cause = cause;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Build a typed error from an HTTP response body + status. The body is
|
|
93
|
+
* expected to follow `ErrorResponse` (see `apps/api/src/lib/api-response.ts`)
|
|
94
|
+
* but we tolerate non-conforming bodies (e.g. a stray HTML 502 from a CDN)
|
|
95
|
+
* by falling back to a generic message.
|
|
96
|
+
*/
|
|
97
|
+
export function fromHttpResponse(status, body, requestId, retryAfterSeconds) {
|
|
98
|
+
const env = isErrorEnvelope(body) ? body.error : null;
|
|
99
|
+
const code = env?.code ?? defaultCodeForStatus(status);
|
|
100
|
+
const type = env?.type ?? defaultTypeForStatus(status);
|
|
101
|
+
const message = env?.message ?? `Kirim API responded with status ${status}.`;
|
|
102
|
+
const param = env?.param;
|
|
103
|
+
const payload = {
|
|
104
|
+
type,
|
|
105
|
+
code,
|
|
106
|
+
message,
|
|
107
|
+
status,
|
|
108
|
+
requestId: env?.request_id ?? requestId,
|
|
109
|
+
param,
|
|
110
|
+
raw: body,
|
|
111
|
+
};
|
|
112
|
+
switch (type) {
|
|
113
|
+
case 'invalid_request_error':
|
|
114
|
+
return new InvalidRequestError(payload);
|
|
115
|
+
case 'authentication_error':
|
|
116
|
+
return new AuthenticationError(payload);
|
|
117
|
+
case 'permission_error':
|
|
118
|
+
return new PermissionError(payload);
|
|
119
|
+
case 'not_found':
|
|
120
|
+
return new NotFoundError(payload);
|
|
121
|
+
case 'conflict':
|
|
122
|
+
return new ConflictError(payload);
|
|
123
|
+
case 'rate_limit_error':
|
|
124
|
+
return new RateLimitError({ ...payload, retryAfter: retryAfterSeconds });
|
|
125
|
+
case 'api_error':
|
|
126
|
+
default:
|
|
127
|
+
return new ApiServerError(payload);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function isErrorEnvelope(body) {
|
|
131
|
+
return (typeof body === 'object' &&
|
|
132
|
+
body !== null &&
|
|
133
|
+
'error' in body &&
|
|
134
|
+
typeof body.error === 'object' &&
|
|
135
|
+
body.error !== null);
|
|
136
|
+
}
|
|
137
|
+
function defaultTypeForStatus(status) {
|
|
138
|
+
if (status === 401)
|
|
139
|
+
return 'authentication_error';
|
|
140
|
+
if (status === 403)
|
|
141
|
+
return 'permission_error';
|
|
142
|
+
if (status === 404)
|
|
143
|
+
return 'not_found';
|
|
144
|
+
if (status === 409)
|
|
145
|
+
return 'conflict';
|
|
146
|
+
if (status === 429)
|
|
147
|
+
return 'rate_limit_error';
|
|
148
|
+
if (status >= 500)
|
|
149
|
+
return 'api_error';
|
|
150
|
+
return 'invalid_request_error';
|
|
151
|
+
}
|
|
152
|
+
function defaultCodeForStatus(status) {
|
|
153
|
+
if (status === 401)
|
|
154
|
+
return 'invalid_api_key';
|
|
155
|
+
if (status === 403)
|
|
156
|
+
return 'feature_not_available';
|
|
157
|
+
if (status === 404)
|
|
158
|
+
return 'resource_not_found';
|
|
159
|
+
if (status === 409)
|
|
160
|
+
return 'conflict';
|
|
161
|
+
if (status === 429)
|
|
162
|
+
return 'rate_limit_exceeded';
|
|
163
|
+
if (status >= 500)
|
|
164
|
+
return 'internal_error';
|
|
165
|
+
return 'invalid_field_value';
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAuBH,4DAA4D;AAC5D,MAAM,OAAO,UAAW,SAAQ,KAAK;IAC1B,IAAI,CAAgB;IACpB,IAAI,CAAQ;IACZ,MAAM,CAAQ;IACd,SAAS,CAAe;IACxB,KAAK,CAAoB;IACzB,GAAG,CAAS;IAErB,YAAY,OAA0B;QACpC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;QACtB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAA;QACxB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAA;QACxB,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAA;QAClC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;QAC1B,IAAI,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAA;IACxB,CAAC;CACF;AAED,MAAM,OAAO,mBAAoB,SAAQ,UAAU;IACjD,YAAY,OAA0B;QACpC,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAA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
|
package/dist/http.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal HTTP client. All resource methods funnel through here so retry,
|
|
3
|
+
* idempotency, timeout, and error mapping are implemented once.
|
|
4
|
+
*
|
|
5
|
+
* Design notes:
|
|
6
|
+
* - Uses Web Standard `fetch` only; no Node-only APIs. Works in Node 18+,
|
|
7
|
+
* Bun, Deno, and Web/edge runtimes.
|
|
8
|
+
* - Auto-injects `Idempotency-Key` on POST so retries are safe by default
|
|
9
|
+
* (matches `apps/api/src/middleware/idempotency.ts`). User-supplied
|
|
10
|
+
* keys override.
|
|
11
|
+
* - Retries on 429 + 502/503/504 + network errors with exponential
|
|
12
|
+
* backoff + jitter. Honors `Retry-After`.
|
|
13
|
+
* - Throws subclasses of `KirimError` for any non-2xx response.
|
|
14
|
+
*/
|
|
15
|
+
export type HttpMethod = 'GET' | 'POST' | 'PATCH' | 'DELETE';
|
|
16
|
+
export interface ClientConfig {
|
|
17
|
+
apiKey: string;
|
|
18
|
+
baseUrl: string;
|
|
19
|
+
timeout: number;
|
|
20
|
+
maxRetries: number;
|
|
21
|
+
fetch: typeof fetch;
|
|
22
|
+
userAgent: string;
|
|
23
|
+
}
|
|
24
|
+
export interface RequestOptions {
|
|
25
|
+
/** Per-call override; otherwise auto-generated for POST. */
|
|
26
|
+
idempotencyKey?: string;
|
|
27
|
+
/** Per-call timeout override (ms). */
|
|
28
|
+
timeout?: number;
|
|
29
|
+
/** Per-call retry override. */
|
|
30
|
+
maxRetries?: number;
|
|
31
|
+
/** Caller-supplied AbortSignal; merged with the timeout signal. */
|
|
32
|
+
signal?: AbortSignal;
|
|
33
|
+
/** Extra headers — last-write-wins over SDK defaults except Authorization. */
|
|
34
|
+
headers?: Record<string, string>;
|
|
35
|
+
}
|
|
36
|
+
export interface RequestArgs {
|
|
37
|
+
method: HttpMethod;
|
|
38
|
+
path: string;
|
|
39
|
+
query?: Record<string, string | number | boolean | undefined | null>;
|
|
40
|
+
body?: unknown;
|
|
41
|
+
options?: RequestOptions;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Public envelope shapes — duplicated here (instead of imported from
|
|
45
|
+
* `generated/`) because the generated file inlines them per-endpoint as
|
|
46
|
+
* anonymous shapes, and we need a single name we can reference.
|
|
47
|
+
*/
|
|
48
|
+
export interface SingleEnvelope<T> {
|
|
49
|
+
data: T;
|
|
50
|
+
request_id: string;
|
|
51
|
+
}
|
|
52
|
+
export interface ListEnvelope<T> {
|
|
53
|
+
data: T[];
|
|
54
|
+
has_more: boolean;
|
|
55
|
+
next_cursor: string | null;
|
|
56
|
+
request_id: string;
|
|
57
|
+
}
|
|
58
|
+
export declare class HttpClient {
|
|
59
|
+
private readonly config;
|
|
60
|
+
constructor(config: ClientConfig);
|
|
61
|
+
/**
|
|
62
|
+
* Execute a request, parse the envelope, and unwrap `data`. Use this for
|
|
63
|
+
* single-resource endpoints. For paginated endpoints use `requestRaw`.
|
|
64
|
+
*/
|
|
65
|
+
requestData<T>(args: RequestArgs): Promise<T>;
|
|
66
|
+
/** Return the raw decoded body (envelope intact). For pagination. */
|
|
67
|
+
requestRaw<T = unknown>(args: RequestArgs): Promise<T>;
|
|
68
|
+
private request;
|
|
69
|
+
private attemptOnce;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAQH,MAAM,MAAM,UAAU,GAAG,KAAK,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAA;AAE5D,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,UAAU,EAAE,MAAM,CAAA;IAClB,KAAK,EAAE,OAAO,KAAK,CAAA;IACnB,SAAS,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