@camunda8/hub-public-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.
package/README.md ADDED
@@ -0,0 +1,124 @@
1
+ # @camunda8/hub-public-api-client
2
+
3
+ A typed TypeScript client for the **Camunda Web Modeler (Hub) public API v2**.
4
+
5
+ The client is generated from the OpenAPI specification that lives at
6
+ [`restapi/public-api/src/main/resources/openapi/v2`](../../../restapi/public-api/src/main/resources/openapi/v2),
7
+ which is the single source of truth for the public API contract. TypeScript
8
+ consumers get typed request/response models and typed methods instead of
9
+ hand-written types that can drift from the published contract.
10
+
11
+ It is built on:
12
+
13
+ - [`openapi-typescript`](https://openapi-ts.dev/) — generates the type
14
+ definitions (`src/generated/schema.ts`) from the spec.
15
+ - [`openapi-fetch`](https://openapi-ts.dev/openapi-fetch/) — a tiny, typed
16
+ `fetch` wrapper used at runtime.
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @camunda8/hub-public-api-client
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Create a client with the API base URL (including the `/api/v2` segment) and a
27
+ bearer token, then call endpoints by path and method. Request bodies, query
28
+ parameters, path parameters, and responses are all fully typed from the spec.
29
+
30
+ ```ts
31
+ import { createHubPublicApiClient } from '@camunda8/hub-public-api-client';
32
+
33
+ const client = createHubPublicApiClient({
34
+ baseUrl: 'https://modeler.camunda.io/api/v2',
35
+ // A static token, or a (possibly async) function returning a fresh one.
36
+ token: async () => fetchAccessToken()
37
+ });
38
+
39
+ // Typed path + body. `data` is the typed response, `error` the typed problem detail.
40
+ const { data, error } = await client.POST('/projects', {
41
+ body: { name: 'My project', workspaceKey: 'my-workspace-key' }
42
+ });
43
+
44
+ if (error) {
45
+ throw new Error(`Request failed: ${error.detail}`);
46
+ }
47
+
48
+ console.log(data.projectKey);
49
+
50
+ // Path parameters are typed too. Every call returns `{ data, error }`.
51
+ const { data: project } = await client.GET('/projects/{projectKey}', {
52
+ params: { path: { projectKey: data.projectKey } }
53
+ });
54
+ ```
55
+
56
+ ### Authentication
57
+
58
+ `token` may be a string or a function returning a string (sync or async). When
59
+ provided, an `Authorization: Bearer <token>` header is attached to every
60
+ request. The function form is resolved on each request, so it can return a
61
+ freshly refreshed token. Omit `token` to handle authentication yourself (e.g.
62
+ via a custom `fetch` or default `headers`, both forwarded to `openapi-fetch`).
63
+
64
+ ### Using the model types
65
+
66
+ The generated models are exported for reuse in your own code:
67
+
68
+ ```ts
69
+ import type { Schemas } from '@camunda8/hub-public-api-client';
70
+
71
+ type Project = Schemas['ProjectResult'];
72
+ ```
73
+
74
+ The lower-level `paths`, `operations`, and `components` types are also exported
75
+ for advanced use cases.
76
+
77
+ ## Generating the client
78
+
79
+ The generated output (`src/generated/schema.ts`) is **not committed** — it is a
80
+ build artifact produced from the spec at build time (the same way the Java
81
+ models are generated into `target/`). It is gitignored and regenerated
82
+ automatically before `tsc` and `build`, so you normally don't run it by hand.
83
+ To generate it explicitly (e.g. for IDE type-checking right after a fresh
84
+ clone):
85
+
86
+ ```bash
87
+ # From this package directory
88
+ npm run generate
89
+
90
+ # Or, from the repository root
91
+ make generate-public-api-client
92
+ ```
93
+
94
+ > **Do not edit the generated output by hand.** Change the spec under
95
+ > `restapi/public-api/src/main/resources/openapi/v2` and regenerate.
96
+
97
+ Published npm consumers never generate anything — they receive the compiled
98
+ `dist/` (built by `prepack`), which already excludes the generated source.
99
+
100
+ ## Staying in sync with the spec
101
+
102
+ Because the client is regenerated on every build, it can never be stale. The
103
+ [`public-api-client-check`](../../../.github/workflows/public-api-client-check.yml)
104
+ GitHub Actions workflow runs on every pull request that touches the spec or this
105
+ package and **regenerates the client and fails if the current spec no longer
106
+ produces a valid, lint-clean, compiling client** — surfacing breaking contract
107
+ changes at build time. (A dedicated workflow is required because the spec lives
108
+ under `restapi/`, outside the frontend workspace, so turbo's `--affected`
109
+ filtering would not otherwise rebuild the client on a spec-only change.)
110
+
111
+ You can run the same check locally:
112
+
113
+ ```bash
114
+ make check-public-api-client
115
+ ```
116
+
117
+ ## Scripts
118
+
119
+ | Script | Description |
120
+ | ------------------ | ----------------------------------------------------------------- |
121
+ | `npm run generate` | Regenerate `src/generated/schema.ts` from the spec. |
122
+ | `npm run build` | Clean, regenerate, then emit JS + declarations to `dist/`. |
123
+ | `npm run tsc` | Regenerate, then type-check without emitting. |
124
+ | `npm run clean` | Remove the `dist/` directory. |
@@ -0,0 +1,36 @@
1
+ import type { Client, ClientOptions } from 'openapi-fetch';
2
+ import type { paths } from './generated/schema.js';
3
+ /**
4
+ * A fully typed client for the Web Modeler (Hub) public API v2. Endpoints are
5
+ * called by path and method, e.g. `client.GET('/projects/{projectKey}', ...)`,
6
+ * with request and response types inferred from the OpenAPI spec.
7
+ */
8
+ export type HubPublicApiClient = Client<paths>;
9
+ /**
10
+ * A bearer token, or a (possibly async) function returning one. The function
11
+ * form is resolved on every request, so it can return a freshly refreshed
12
+ * token.
13
+ */
14
+ export type BearerTokenProvider = string | (() => string | undefined | Promise<string | undefined>);
15
+ export interface CreateHubPublicApiClientOptions extends ClientOptions {
16
+ /**
17
+ * Base URL of the API, including the `/api/v2` path segment, e.g.
18
+ * `https://modeler.camunda.io/api/v2`.
19
+ */
20
+ baseUrl: string;
21
+ /**
22
+ * Bearer token (JWT) used to authenticate requests. When provided, an
23
+ * `Authorization: Bearer <token>` header is attached to every request.
24
+ */
25
+ token?: BearerTokenProvider;
26
+ }
27
+ /**
28
+ * Creates a typed client for the Web Modeler (Hub) public API v2.
29
+ *
30
+ * @param options - Client configuration. `baseUrl` is required; `token`
31
+ * enables bearer authentication. Any other `openapi-fetch` option (e.g. a
32
+ * custom `fetch` or default `headers`) is forwarded.
33
+ * @returns A typed client exposing one method per HTTP verb.
34
+ */
35
+ export declare function createHubPublicApiClient(options: CreateHubPublicApiClientOptions): HubPublicApiClient;
36
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE3D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAEnD;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;AAE/C;;;;GAIG;AACH,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,CAAC,MAAM,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAEpG,MAAM,WAAW,+BAAgC,SAAQ,aAAa;IACpE;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,KAAK,CAAC,EAAE,mBAAmB,CAAC;CAC7B;AAED;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,+BAA+B,GAAG,kBAAkB,CAiBrG"}
package/dist/client.js ADDED
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
3
+ * one or more contributor license agreements. See the NOTICE file distributed
4
+ * with this work for additional information regarding copyright ownership.
5
+ * Licensed under the Camunda License 1.0. You may not use this file
6
+ * except in compliance with the Camunda License 1.0.
7
+ */
8
+ import createClient from 'openapi-fetch';
9
+ /**
10
+ * Creates a typed client for the Web Modeler (Hub) public API v2.
11
+ *
12
+ * @param options - Client configuration. `baseUrl` is required; `token`
13
+ * enables bearer authentication. Any other `openapi-fetch` option (e.g. a
14
+ * custom `fetch` or default `headers`) is forwarded.
15
+ * @returns A typed client exposing one method per HTTP verb.
16
+ */
17
+ export function createHubPublicApiClient(options) {
18
+ const { token, ...clientOptions } = options;
19
+ const client = createClient(clientOptions);
20
+ if (token !== undefined) {
21
+ client.use({
22
+ async onRequest({ request }) {
23
+ const value = typeof token === 'function' ? await token() : token;
24
+ if (value) {
25
+ request.headers.set('Authorization', `Bearer ${value}`);
26
+ }
27
+ return request;
28
+ }
29
+ });
30
+ }
31
+ return client;
32
+ }
33
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,YAAY,MAAM,eAAe,CAAC;AAgCzC;;;;;;;GAOG;AACH,MAAM,UAAU,wBAAwB,CAAC,OAAwC;IAC/E,MAAM,EAAE,KAAK,EAAE,GAAG,aAAa,EAAE,GAAG,OAAO,CAAC;IAC5C,MAAM,MAAM,GAAG,YAAY,CAAQ,aAAa,CAAC,CAAC;IAElD,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACxB,MAAM,CAAC,GAAG,CAAC;YACT,KAAK,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE;gBACzB,MAAM,KAAK,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;gBAClE,IAAI,KAAK,EAAE,CAAC;oBACV,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,UAAU,KAAK,EAAE,CAAC,CAAC;gBAC1D,CAAC;gBACD,OAAO,OAAO,CAAC;YACjB,CAAC;SACF,CAAC,CAAC;IACL,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC"}