@tietokilta/pakuvaraus-client 0.0.0 → 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,66 @@
1
+ # @tietokilta/pakuvaraus-client
2
+
3
+ Typed `fetch` client for the pakuvaraus public API. The types in `schema.d.ts`
4
+ are generated by [openapi-typescript](https://openapi-ts.dev) from the API's
5
+ OpenAPI spec, which is generated from the Rust handlers with
6
+ [utoipa](https://github.com/juhaku/utoipa). Requests go through
7
+ [openapi-fetch](https://openapi-ts.dev/openapi-fetch/). Neither generated file
8
+ is committed.
9
+
10
+ ```ts
11
+ import { createPakuvarausClient, type CreateReservation } from "@tietokilta/pakuvaraus-client";
12
+
13
+ const api = createPakuvarausClient({ baseUrl: "https://pakuvaraus.example" });
14
+
15
+ const { data, error } = await api.GET("/reservations");
16
+
17
+ const { response } = await api.POST("/reservations", {
18
+ body: reservation satisfies CreateReservation,
19
+ });
20
+ if (!response.ok) {
21
+ // handle the error
22
+ }
23
+ ```
24
+
25
+ Check `response.ok` after `POST /reservations`, not `error`. A successful
26
+ request has an empty body, so `data` and `error` are both `undefined`. They are
27
+ also both `undefined` for an empty-bodied failure, such as a 404 or 405 from a
28
+ wrong `baseUrl`.
29
+
30
+ `error` is typed per status: `ErrorResponse` (`{ error: string }`) for 409 and
31
+ 500, and a plain-text string for 400, 413, 415 and 422 (axum's body
32
+ rejections).
33
+
34
+ Cancelling isn't part of this API. The confirmation email links to
35
+ pakuvaraus's own HTML cancel page.
36
+
37
+ ## Building
38
+
39
+ ```sh
40
+ nix build .#openapi-spec # result: the OpenAPI spec
41
+ nix build .#api-client # result/: the npm package, ready to publish
42
+ ```
43
+
44
+ `nix flake check` builds both, so a handler change that breaks the client's
45
+ types fails CI. The running server also serves the spec at `/openapi.json`.
46
+
47
+ To work on `index.js`/`index.d.ts` locally, in `nix develop`:
48
+
49
+ ```sh
50
+ nix build .#openapi-spec -o client/openapi.json
51
+ cd client && pnpm install && pnpm run generate && pnpm run typecheck
52
+ ```
53
+
54
+ ## Releasing
55
+
56
+ On every push to `main`, after the Azure deploy succeeds, the `publish-client` job in
57
+ [`release.yml`](../.github/workflows/release.yml) builds `.#api-client`. When its
58
+ contents differ from the latest published version, it publishes the next
59
+ patch version with trusted publishing. It takes major and minor from
60
+ `version` in `package.json`, and ignores the patch there.
61
+ For a breaking API change, bump the minor version (or the major after 1.0) in
62
+ `package.json`, since `^0.1.0` only accepts new `0.1.x` patches. The npm
63
+ package's settings must list `release.yml` with environment `npm-publish` as a
64
+ trusted publisher that is allowed to run `npm publish`. That GitHub environment
65
+ must only allow the `main` branch. A run only publishes if its commit is still
66
+ the head of `main`, so rerunning an old run does nothing.
package/index.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { Client, ClientOptions } from "openapi-fetch";
2
+ import type { paths } from "./schema.js";
3
+
4
+ export type * from "./schema.js";
5
+
6
+ export type PakuvarausClient = Client<paths>;
7
+
8
+ /**
9
+ * Create a client for the pakuvaraus API. Pass `baseUrl`, the API origin
10
+ * such as `https://pakuvaraus.tietokilta.fi`, and optionally a custom `fetch`.
11
+ */
12
+ export declare function createPakuvarausClient(
13
+ options?: ClientOptions,
14
+ ): PakuvarausClient;
package/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import createClient from "openapi-fetch";
2
+
3
+ /** @type {import("./index.d.ts").createPakuvarausClient} */
4
+ export const createPakuvarausClient = (options) => createClient(options);
package/package.json CHANGED
@@ -1,13 +1,36 @@
1
1
  {
2
2
  "name": "@tietokilta/pakuvaraus-client",
3
- "version": "0.0.0",
4
- "description": "",
5
- "main": "index.js",
3
+ "version": "0.1.0",
4
+ "description": "Typed fetch client for the pakuvaraus API, generated from its OpenAPI spec",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/Tietokilta/pakuvaraus.git",
8
+ "directory": "client"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./index.d.ts",
17
+ "default": "./index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "index.js",
22
+ "index.d.ts",
23
+ "schema.d.ts"
24
+ ],
6
25
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
26
+ "generate": "openapi-typescript openapi.json --root-types --root-types-no-schema-prefix --output schema.d.ts",
27
+ "typecheck": "tsc"
28
+ },
29
+ "dependencies": {
30
+ "openapi-fetch": "^0.17.0"
8
31
  },
9
- "keywords": [],
10
- "author": "",
11
- "license": "ISC",
12
- "type": "commonjs"
32
+ "devDependencies": {
33
+ "openapi-typescript": "7.13.0",
34
+ "typescript": "^5.9.3"
35
+ }
13
36
  }
package/schema.d.ts ADDED
@@ -0,0 +1,186 @@
1
+ /**
2
+ * This file was auto-generated by openapi-typescript.
3
+ * Do not make direct changes to the file.
4
+ */
5
+
6
+ export interface paths {
7
+ "/reservations": {
8
+ parameters: {
9
+ query?: never;
10
+ header?: never;
11
+ path?: never;
12
+ cookie?: never;
13
+ };
14
+ /** List the time slots of all reservations. */
15
+ get: operations["listReservations"];
16
+ put?: never;
17
+ /**
18
+ * Request a reservation.
19
+ * @description Generates the rental contract, emails it to the approver, and emails the
20
+ * requester a confirmation with a cancel link. The reservation stays pending
21
+ * until an admin approves it.
22
+ */
23
+ post: operations["createReservation"];
24
+ delete?: never;
25
+ options?: never;
26
+ head?: never;
27
+ patch?: never;
28
+ trace?: never;
29
+ };
30
+ }
31
+ export type webhooks = Record<string, never>;
32
+ export interface components {
33
+ schemas: {
34
+ Address: string;
35
+ CreateReservation: {
36
+ /** Format: date-time */
37
+ end_time: string;
38
+ /** Format: date-time */
39
+ start_time: string;
40
+ } & {
41
+ address: components["schemas"]["Address"];
42
+ email: components["schemas"]["Email"];
43
+ full_name: components["schemas"]["FullName"];
44
+ /** @description Reserved for guild use rather than a member's personal use */
45
+ internal: boolean;
46
+ phone_number: components["schemas"]["Phone"];
47
+ used_outside_finland: boolean;
48
+ };
49
+ /** Format: email */
50
+ Email: string;
51
+ /** @description Body of every error response that the app itself produces. */
52
+ ErrorResponse: {
53
+ /** @description Human-readable description of the error */
54
+ error: string;
55
+ };
56
+ FullName: string;
57
+ /** @description Phone number; a number without a country code is read as Finnish */
58
+ Phone: string;
59
+ ReservationItem: {
60
+ /** Format: date-time */
61
+ end_time: string;
62
+ /** Format: date-time */
63
+ start_time: string;
64
+ };
65
+ };
66
+ responses: never;
67
+ parameters: never;
68
+ requestBodies: never;
69
+ headers: never;
70
+ pathItems: never;
71
+ }
72
+ export type Address = components['schemas']['Address'];
73
+ export type CreateReservation = components['schemas']['CreateReservation'];
74
+ export type Email = components['schemas']['Email'];
75
+ export type ErrorResponse = components['schemas']['ErrorResponse'];
76
+ export type FullName = components['schemas']['FullName'];
77
+ export type Phone = components['schemas']['Phone'];
78
+ export type ReservationItem = components['schemas']['ReservationItem'];
79
+ export type $defs = Record<string, never>;
80
+ export interface operations {
81
+ listReservations: {
82
+ parameters: {
83
+ query?: never;
84
+ header?: never;
85
+ path?: never;
86
+ cookie?: never;
87
+ };
88
+ requestBody?: never;
89
+ responses: {
90
+ /** @description Every reservation, including pending and cancelled ones */
91
+ 200: {
92
+ headers: {
93
+ [name: string]: unknown;
94
+ };
95
+ content: {
96
+ "application/json": components["schemas"]["ReservationItem"][];
97
+ };
98
+ };
99
+ /** @description Internal server error */
100
+ 500: {
101
+ headers: {
102
+ [name: string]: unknown;
103
+ };
104
+ content: {
105
+ "application/json": components["schemas"]["ErrorResponse"];
106
+ };
107
+ };
108
+ };
109
+ };
110
+ createReservation: {
111
+ parameters: {
112
+ query?: never;
113
+ header?: never;
114
+ path?: never;
115
+ cookie?: never;
116
+ };
117
+ requestBody: {
118
+ content: {
119
+ "application/json": components["schemas"]["CreateReservation"];
120
+ };
121
+ };
122
+ responses: {
123
+ /** @description Reservation requested. The response body is empty. */
124
+ 200: {
125
+ headers: {
126
+ [name: string]: unknown;
127
+ };
128
+ content?: never;
129
+ };
130
+ /** @description The body is not valid JSON */
131
+ 400: {
132
+ headers: {
133
+ [name: string]: unknown;
134
+ };
135
+ content: {
136
+ "text/plain": string;
137
+ };
138
+ };
139
+ /** @description The time overlaps another reservation */
140
+ 409: {
141
+ headers: {
142
+ [name: string]: unknown;
143
+ };
144
+ content: {
145
+ "application/json": components["schemas"]["ErrorResponse"];
146
+ };
147
+ };
148
+ /** @description The body is larger than the server accepts */
149
+ 413: {
150
+ headers: {
151
+ [name: string]: unknown;
152
+ };
153
+ content: {
154
+ "text/plain": string;
155
+ };
156
+ };
157
+ /** @description The `Content-Type` header is not `application/json` */
158
+ 415: {
159
+ headers: {
160
+ [name: string]: unknown;
161
+ };
162
+ content: {
163
+ "text/plain": string;
164
+ };
165
+ };
166
+ /** @description A field is missing or invalid; the message names the problem */
167
+ 422: {
168
+ headers: {
169
+ [name: string]: unknown;
170
+ };
171
+ content: {
172
+ "text/plain": string;
173
+ };
174
+ };
175
+ /** @description Internal server error */
176
+ 500: {
177
+ headers: {
178
+ [name: string]: unknown;
179
+ };
180
+ content: {
181
+ "application/json": components["schemas"]["ErrorResponse"];
182
+ };
183
+ };
184
+ };
185
+ };
186
+ }