@airfone/api 0.0.0-stage → 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AirFone
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 CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # @airfone/api
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A typed client for the AirFone API. The types are generated from the public contract
4
+ (`backend/openapi/airfone-v1.yaml`), so a path, its parameters, its body and its response are checked
5
+ by the compiler.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @airfone/api
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```ts
16
+ import { createClient } from '@airfone/api';
17
+
18
+ const airfone = createClient({ token: process.env.AIRFONE_KEY! }); // af_sk_... or afp_...
19
+
20
+ const page = await airfone.get('/v1/agents', { query: { limit: 20 } });
21
+ await airfone.post('/v1/messages', { body: { /* typed from the contract */ }, idempotencyKey: crypto.randomUUID() });
22
+ ```
23
+
24
+ Errors are thrown as `AirFoneError` with `status`, `code`, `message` and `requestId`.
25
+
26
+ `paths`, `components` and `operations` are exported as types, for code that wants the contract's
27
+ shapes without the client.
28
+
29
+ ## Regenerate the types
30
+
31
+ ```sh
32
+ pnpm --filter @airfone/api generate
33
+ ```
34
+
35
+ CI regenerates them and fails when `src/schema.d.ts` is not what the contract produces.
36
+
37
+ ## License
38
+
39
+ MIT
@@ -0,0 +1,51 @@
1
+ import type { paths } from './schema.js';
2
+ export type { paths, components, operations } from './schema.js';
3
+ type Method = 'get' | 'post' | 'put' | 'patch' | 'delete';
4
+ type Op<P extends keyof paths, M extends Method> = M extends keyof paths[P] ? NonNullable<paths[P][M]> : never;
5
+ type Json<T> = T extends {
6
+ content: {
7
+ 'application/json': infer B;
8
+ };
9
+ } ? B : undefined;
10
+ type Ok<O> = O extends {
11
+ responses: infer R;
12
+ } ? Json<R[Extract<keyof R, 200 | 201 | 202 | '200' | '201' | '202'>]> : never;
13
+ type Body<O> = O extends {
14
+ requestBody?: infer B;
15
+ } ? Json<NonNullable<B>> : undefined;
16
+ type Params<O> = O extends {
17
+ parameters: infer P;
18
+ } ? P : never;
19
+ export interface ClientOptions {
20
+ /** A secret or restricted key (`af_sk_…`) or a personal access token (`afp_…`). */
21
+ token: string;
22
+ /** Defaults to https://app.airfone.app. */
23
+ baseUrl?: string;
24
+ /** Pins the API version (`AirFone-Version`). */
25
+ version?: string;
26
+ fetch?: typeof fetch;
27
+ }
28
+ /** An error the API answered with: `{ error: { code, message } }`. */
29
+ export declare class AirFoneError extends Error {
30
+ readonly status: number;
31
+ readonly code: string;
32
+ readonly requestId?: string | undefined;
33
+ constructor(status: number, code: string, message: string, requestId?: string | undefined);
34
+ }
35
+ /** What a request can carry besides its path. */
36
+ export interface RequestInit<P extends keyof paths, M extends Method> {
37
+ path?: Record<string, string>;
38
+ query?: Record<string, string | number | boolean | undefined>;
39
+ body?: Body<Op<P, M>>;
40
+ idempotencyKey?: string;
41
+ }
42
+ type Verb<M extends Method> = <P extends keyof paths & string>(path: P, init?: RequestInit<P, M>) => Promise<Ok<Op<P, M>>>;
43
+ export interface Client {
44
+ get: Verb<'get'>;
45
+ post: Verb<'post'>;
46
+ put: Verb<'put'>;
47
+ patch: Verb<'patch'>;
48
+ delete: Verb<'delete'>;
49
+ }
50
+ export declare function createClient(options: ClientOptions): Client;
51
+ export type { Params };
package/dist/index.js ADDED
@@ -0,0 +1,48 @@
1
+ /** An error the API answered with: `{ error: { code, message } }`. */
2
+ export class AirFoneError extends Error {
3
+ status;
4
+ code;
5
+ requestId;
6
+ constructor(status, code, message, requestId) {
7
+ super(message);
8
+ this.status = status;
9
+ this.code = code;
10
+ this.requestId = requestId;
11
+ this.name = 'AirFoneError';
12
+ }
13
+ }
14
+ export function createClient(options) {
15
+ const base = (options.baseUrl ?? 'https://app.airfone.app').replace(/\/$/, '');
16
+ const doFetch = options.fetch ?? fetch;
17
+ async function call(method, path, init = {}) {
18
+ let url = path.replace(/\{(\w+)\}/g, (_, k) => encodeURIComponent(init.path?.[k] ?? ''));
19
+ const q = new URLSearchParams();
20
+ for (const [k, v] of Object.entries(init.query ?? {}))
21
+ if (v !== undefined)
22
+ q.set(k, String(v));
23
+ if ([...q].length)
24
+ url += `?${q}`;
25
+ const headers = { Authorization: `Bearer ${options.token}`, Accept: 'application/json' };
26
+ if (options.version)
27
+ headers['AirFone-Version'] = options.version;
28
+ if (init.idempotencyKey)
29
+ headers['Idempotency-Key'] = init.idempotencyKey;
30
+ if (init.body !== undefined)
31
+ headers['Content-Type'] = 'application/json';
32
+ const res = await doFetch(base + url, { method: method.toUpperCase(), headers, body: init.body === undefined ? undefined : JSON.stringify(init.body) });
33
+ const text = await res.text();
34
+ const data = text ? JSON.parse(text) : undefined;
35
+ if (!res.ok) {
36
+ const e = data?.error ?? {};
37
+ throw new AirFoneError(res.status, e.code ?? 'error', e.message ?? res.statusText, res.headers.get('Request-Id') ?? undefined);
38
+ }
39
+ return data;
40
+ }
41
+ return {
42
+ get: (path, init) => call('get', path, init),
43
+ post: (path, init) => call('post', path, init),
44
+ put: (path, init) => call('put', path, init),
45
+ patch: (path, init) => call('patch', path, init),
46
+ delete: (path, init) => call('delete', path, init),
47
+ };
48
+ }