@dsptch-work/api-client 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.
Files changed (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +130 -0
  3. package/dist/gen/client/client.gen.d.ts +2 -0
  4. package/dist/gen/client/client.gen.js +216 -0
  5. package/dist/gen/client/index.d.ts +8 -0
  6. package/dist/gen/client/index.js +6 -0
  7. package/dist/gen/client/types.gen.d.ts +120 -0
  8. package/dist/gen/client/types.gen.js +2 -0
  9. package/dist/gen/client/utils.gen.d.ts +37 -0
  10. package/dist/gen/client/utils.gen.js +228 -0
  11. package/dist/gen/client.gen.d.ts +12 -0
  12. package/dist/gen/client.gen.js +3 -0
  13. package/dist/gen/core/auth.gen.d.ts +18 -0
  14. package/dist/gen/core/auth.gen.js +14 -0
  15. package/dist/gen/core/bodySerializer.gen.d.ts +25 -0
  16. package/dist/gen/core/bodySerializer.gen.js +57 -0
  17. package/dist/gen/core/params.gen.d.ts +43 -0
  18. package/dist/gen/core/params.gen.js +100 -0
  19. package/dist/gen/core/pathSerializer.gen.d.ts +33 -0
  20. package/dist/gen/core/pathSerializer.gen.js +106 -0
  21. package/dist/gen/core/queryKeySerializer.gen.d.ts +18 -0
  22. package/dist/gen/core/queryKeySerializer.gen.js +92 -0
  23. package/dist/gen/core/serverSentEvents.gen.d.ts +71 -0
  24. package/dist/gen/core/serverSentEvents.gen.js +132 -0
  25. package/dist/gen/core/types.gen.d.ts +78 -0
  26. package/dist/gen/core/types.gen.js +2 -0
  27. package/dist/gen/core/utils.gen.d.ts +19 -0
  28. package/dist/gen/core/utils.gen.js +87 -0
  29. package/dist/gen/index.d.ts +2 -0
  30. package/dist/gen/index.js +2 -0
  31. package/dist/gen/sdk.gen.d.ts +2686 -0
  32. package/dist/gen/sdk.gen.js +4719 -0
  33. package/dist/gen/types.gen.d.ts +16974 -0
  34. package/dist/gen/types.gen.js +2 -0
  35. package/dist/gen/zod.gen.d.ts +16833 -0
  36. package/dist/gen/zod.gen.js +6478 -0
  37. package/dist/index.d.ts +2 -0
  38. package/dist/index.js +5 -0
  39. package/dist/pagination.d.ts +36 -0
  40. package/dist/pagination.js +60 -0
  41. package/package.json +63 -0
@@ -0,0 +1,2 @@
1
+ export * from './gen/index.js';
2
+ export * from './pagination.js';
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ // Package root. Re-exports the generated client from src/gen/ (owned by the generator —
2
+ // see openapi-ts.config.ts) so consumers import from '@dsptch-work/api-client'. This file and
3
+ // its siblings in src/ are hand-authored and survive regeneration, which only touches src/gen/.
4
+ export * from './gen/index.js';
5
+ export * from './pagination.js';
@@ -0,0 +1,36 @@
1
+ import type { Meta, Links } from './gen/index.js';
2
+ /** The keyset envelope every list endpoint returns as its success body. */
3
+ interface Page<TItem> {
4
+ data?: TItem[];
5
+ meta?: Meta;
6
+ links?: Links;
7
+ }
8
+ type ListMethod = (options: never) => Promise<{
9
+ data?: Page<unknown>;
10
+ error?: unknown;
11
+ }>;
12
+ /** The element type of a list method's page — e.g. `JobResponseObject` for `Jobs.listJobs`. */
13
+ type ItemOf<TList extends ListMethod> = NonNullable<NonNullable<Awaited<ReturnType<TList>>['data']>['data']>[number];
14
+ /**
15
+ * Walk every page of a cursor-paginated list endpoint, yielding one item at a time.
16
+ *
17
+ * Pass a generated list method (e.g. `Jobs.listJobs`) and its options; the item type is inferred
18
+ * from the method, and the options keep the method's own shape — required where the endpoint needs
19
+ * a path id, optional otherwise. The generator sends `page[cursor]` from each page's `next_cursor`
20
+ * and stops when the server reports no more pages (`next_cursor == null`).
21
+ *
22
+ * Assumes the client's default `responseStyle` ('fields', i.e. `{ data, error }`). If the endpoint
23
+ * returns an error, it is thrown.
24
+ *
25
+ * @example
26
+ * for await (const job of paginate(Jobs.listJobs, { query: { 'filter[project_id]': id } })) {
27
+ * process(job);
28
+ * }
29
+ */
30
+ export declare function paginate<TList extends ListMethod>(list: TList, ...rest: Parameters<TList>): AsyncGenerator<ItemOf<TList>>;
31
+ /**
32
+ * Drain an async iterable into an array. Convenience for the common case where every page fits in
33
+ * memory: `const jobs = await collect(paginate(Jobs.listJobs))`.
34
+ */
35
+ export declare function collect<TItem>(source: AsyncIterable<TItem>): Promise<TItem[]>;
36
+ export {};
@@ -0,0 +1,60 @@
1
+ // Hand-written helper (survives regeneration — only src/gen/ is regenerated).
2
+ // Every list endpoint returns the same keyset envelope: { data, meta.page.next_cursor, links.next }.
3
+ // A null next_cursor is the last page. `paginate` turns that shared contract into one async
4
+ // generator, so callers write `for await (const item of paginate(Client.listX))` instead of
5
+ // hand-rolling a `while (next_cursor)` loop per resource.
6
+ /**
7
+ * Walk every page of a cursor-paginated list endpoint, yielding one item at a time.
8
+ *
9
+ * Pass a generated list method (e.g. `Jobs.listJobs`) and its options; the item type is inferred
10
+ * from the method, and the options keep the method's own shape — required where the endpoint needs
11
+ * a path id, optional otherwise. The generator sends `page[cursor]` from each page's `next_cursor`
12
+ * and stops when the server reports no more pages (`next_cursor == null`).
13
+ *
14
+ * Assumes the client's default `responseStyle` ('fields', i.e. `{ data, error }`). If the endpoint
15
+ * returns an error, it is thrown.
16
+ *
17
+ * @example
18
+ * for await (const job of paginate(Jobs.listJobs, { query: { 'filter[project_id]': id } })) {
19
+ * process(job);
20
+ * }
21
+ */
22
+ export async function* paginate(list, ...rest) {
23
+ // TList is opaque inside the body (its parameter narrows to `never`), so call it through a plain
24
+ // fields-style signature. The public signature above is what keeps callers type-safe.
25
+ const call = list;
26
+ // Under the constraint `Parameters<TList>` is a one-tuple of `never`, so the rest element reads as
27
+ // always present; a concrete method whose options are optional really is callable with none.
28
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
29
+ const base = (rest[0] ?? {});
30
+ let query = { ...(base.query ?? {}) };
31
+ for (;;) {
32
+ const sent = query['page[cursor]'];
33
+ const { data, error } = await call({ ...base, query });
34
+ // The endpoint's error is the API's own JSON body, not an Error instance; surfacing it verbatim
35
+ // is the documented contract so callers can read the server's shape.
36
+ // eslint-disable-next-line @typescript-eslint/only-throw-error
37
+ if (error)
38
+ throw error;
39
+ for (const item of data?.data ?? [])
40
+ yield item;
41
+ const next = data?.meta?.page?.next_cursor;
42
+ if (next == null)
43
+ return;
44
+ // The server should never hand back the cursor we just sent; if it does, following it would
45
+ // loop forever. There is no page cap otherwise, so guard against it explicitly.
46
+ if (next === sent)
47
+ throw new Error(`Pagination cursor did not advance ("${next}"); aborting.`);
48
+ query = { ...query, 'page[cursor]': next };
49
+ }
50
+ }
51
+ /**
52
+ * Drain an async iterable into an array. Convenience for the common case where every page fits in
53
+ * memory: `const jobs = await collect(paginate(Jobs.listJobs))`.
54
+ */
55
+ export async function collect(source) {
56
+ const items = [];
57
+ for await (const item of source)
58
+ items.push(item);
59
+ return items;
60
+ }
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@dsptch-work/api-client",
3
+ "version": "0.1.0",
4
+ "description": "Generated TypeScript client for the DSPTCH REST API.",
5
+ "keywords": [
6
+ "dsptch",
7
+ "api",
8
+ "client",
9
+ "sdk",
10
+ "openapi",
11
+ "rest",
12
+ "typescript"
13
+ ],
14
+ "homepage": "https://dsptch.app/support/api/api-v-1",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/rnwbllabs/dsptch-api.git",
18
+ "directory": "clients/typescript"
19
+ },
20
+ "license": "MIT",
21
+ "type": "module",
22
+ "sideEffects": false,
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js"
27
+ },
28
+ "./client": {
29
+ "types": "./dist/gen/client.gen.d.ts",
30
+ "import": "./dist/gen/client.gen.js"
31
+ },
32
+ "./zod": {
33
+ "types": "./dist/gen/zod.gen.d.ts",
34
+ "import": "./dist/gen/zod.gen.js"
35
+ }
36
+ },
37
+ "files": [
38
+ "dist"
39
+ ],
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "dependencies": {
44
+ "zod": "4.3.6"
45
+ },
46
+ "devDependencies": {
47
+ "@hey-api/openapi-ts": "^0.97.3",
48
+ "publint": "^0.3.21",
49
+ "typescript": "^6.0.3",
50
+ "vitest": "^4.1.10"
51
+ },
52
+ "scripts": {
53
+ "generate": "openapi-ts",
54
+ "build": "tsc -p tsconfig.build.json",
55
+ "typecheck": "tsc --noEmit",
56
+ "lint:package": "publint",
57
+ "test": "vitest run",
58
+ "test:watch": "vitest"
59
+ },
60
+ "main": "./dist/index.js",
61
+ "module": "./dist/index.js",
62
+ "types": "./dist/index.d.ts"
63
+ }