@mittwald/api-client 0.0.0-development-04b7288-20240610

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 (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +274 -0
  3. package/package.json +83 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Mittwald CM Service GmbH & Co. KG and contributors
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,274 @@
1
+ # mittwald API JavaScript client
2
+
3
+ This package contains a (mostly auto-generated) JavaScript client for the
4
+ mittwald API.
5
+
6
+ ## License
7
+
8
+ Copyright (c) 2023 Mittwald CM Service GmbH & Co. KG and contributors
9
+
10
+ This project (and all NPM packages) therein is licensed under the MIT License;
11
+ see the [LICENSE](../../LICENSE) file for details.
12
+
13
+ ## Table of contents
14
+
15
+ - [Installing](#installing)
16
+ - [Usage](#usage)
17
+ - [Setting request parameters](#setting-request-parameters)
18
+ - [Path parameters](#path-parameters)
19
+ - [Headers, query parameters and request body](#headers-query-parameters-and-request-body)
20
+ - [Example](#example)
21
+ - [Usage in React](#usage-in-react)
22
+ - [Installation](#installation)
23
+ - [Setup](#setup)
24
+ - [Usage](#usage-1)
25
+ - [Example](#example-1)
26
+ - [API documentation](#api-documentation)
27
+ - [Accessing the underlying Axios instance](#accessing-the-underlying-axios-instance)
28
+ - [Adding custom HTTP headers](#adding-custom-http-headers)
29
+ - [Intercepting requests or responses](#intercepting-requests-or-responses)
30
+ - [Usage with TypeScript](#usage-with-typescript)
31
+ - [Importing types](#importing-types)
32
+ - [Migrating from package version V2 to V3](#migrating-from-package-version-v2-to-v3)
33
+
34
+ ## Installing
35
+
36
+ You can install this package from the regular NPM registry:
37
+
38
+ ```shell
39
+ yarn add @mittwald/api-client
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ Import the client:
45
+
46
+ ```typescript
47
+ import { MittwaldAPIV2Client } from "@mittwald/api-client";
48
+ ```
49
+
50
+ To create a client instance you can use one of the following factory method for
51
+ different types of authentication:
52
+
53
+ 1. `MittwaldAPIClient.newUnauthenticated()`
54
+ 2. `MittwaldAPIClient.newWithToken(apiToken: string)` (recommended)
55
+ 3. `MittwaldAPIClient.newWithCredentials(email: string, password: string)` (does
56
+ actually perform a login in the background and thus returns a promise)
57
+
58
+ Have a look at our [API introduction][api-getting-started] for more information
59
+ on how to obtain an API token and how to get started with the API.
60
+
61
+ ### Setting request parameters
62
+
63
+ API requests may require these type of parameters:
64
+
65
+ - path parameters
66
+ - headers
67
+ - query parameters
68
+ - request body
69
+
70
+ #### Path parameters
71
+
72
+ Path parameters are variable parts of a URL path. They are typically used to
73
+ point to a specific resource within a collection, such as a project identified
74
+ by ID. A URL can have several path parameters, each denoted with curly braces
75
+ `{ }`.
76
+
77
+ ```
78
+ /v2/projects/{projectId}
79
+ ```
80
+
81
+ Path parameters **are required** and must be directly defined inside the request
82
+ object.
83
+
84
+ ```javascript
85
+ // Setting the projectId path parameters
86
+ const project = await mittwaldApi.project.getProject({
87
+ projectId: "p-xxxxx",
88
+ });
89
+ ```
90
+
91
+ #### Headers, query parameters and request body
92
+
93
+ Depending on the operation, you must configure additional parameters, such as
94
+ `queryParameters` (URL query parameters), `headers` (HTTP headers), and `data`
95
+ (request body).
96
+
97
+ The operations and their parameters are documented in the
98
+ [API documentation](https://developer.mittwald.de/reference/v2/).
99
+
100
+ When using TypeScript all parameter schemas are reflected by the request type,
101
+ so you will get compile errors, when a request does not match the schema.
102
+
103
+ ```javascript
104
+ const response = await mittwaldApi.category.operation({
105
+ // path parameters
106
+ pathParameter1: "param1",
107
+ pathParameter2: "param2",
108
+ // parameters in header
109
+ headers: {
110
+ "x-header": "header",
111
+ },
112
+ // query parameters
113
+ queryParameters: {
114
+ queryParameter1: "queryParam1",
115
+ queryParameter2: "queryParam2",
116
+ },
117
+ // JSON object in request body
118
+ data: {
119
+ data1: "data1",
120
+ data2: "data2",
121
+ },
122
+ });
123
+ ```
124
+
125
+ ## Example
126
+
127
+ ```typescript
128
+ import { MittwaldAPIV2Client } from "@mittwald/api-client";
129
+
130
+ const mittwaldApi = MittwaldAPIClient.newWithToken("your-access-token");
131
+
132
+ const projects = await mittwaldApi.project.listProjects();
133
+ ```
134
+
135
+ ## Usage in React
136
+
137
+ This package also provides a client aligned to be used in React components. It
138
+ uses
139
+ [`@mittwald/react-use-promise`](https://www.npmjs.com/package/@mittwald/react-use-promise)
140
+ to encapsulate the asynchronous API functions into AsyncResources. More details
141
+ about how to use AsyncResources see the
142
+ [package documentation](https://www.npmjs.com/package/@mittwald/react-use-promise#terminology).
143
+
144
+ ### Installation
145
+
146
+ To use the React client you have to install the additional
147
+ `@mittwald/react-use-promise` package:
148
+
149
+ ```shell
150
+ yarn add @mittwald/react-use-promise
151
+ ```
152
+
153
+ ### Setup
154
+
155
+ To create a React client instance, you first need an instance of the regular
156
+ (promise-based) client. Then you can use the `.fromBaseClient(api)` method to
157
+ build the React client.
158
+
159
+ ```javascript
160
+ const api = MittwaldAPIV2Client.newUnauthenticated();
161
+ const apiReact = MittwaldAPIV2ClientReact.fromBaseClient(api);
162
+ ```
163
+
164
+ ### Usage
165
+
166
+ The React client has an equivalent for every GET method of the regular client.
167
+ The methods returning an AsyncResource that can be used to get the API
168
+ responses.
169
+
170
+ If the response is not OK (status 200), an `ApiClientError` will be thrown.
171
+ Errors can be handled by using error-boundaries. See the
172
+ [error handling section](https://www.npmjs.com/package/@mittwald/react-use-promise#error-handling)
173
+ for more details.
174
+
175
+ ### Example
176
+
177
+ ```javascript jsx
178
+ import { MittwaldAPIV2Client } from "@mittwald/api-client";
179
+ import { MittwaldAPIV2ClientReact } from "@mittwald/api-client/react";
180
+
181
+ const api = MittwaldAPIV2Client.newUnauthenticated();
182
+ const apiReact = MittwaldAPIV2ClientReact.fromBaseClient(api);
183
+
184
+ const ProjectsList = () => {
185
+ // apiReact.project.listProjects() returns an AsyncResource that can be "used"
186
+ const projects = apiReact.project.listProjects().use({
187
+ autoRefresh: {
188
+ seconds: 30,
189
+ },
190
+ });
191
+
192
+ return (
193
+ <ul>
194
+ {projects.map((p) => (
195
+ <li key={p.id}>{p.description}</li>
196
+ ))}
197
+ </ul>
198
+ );
199
+ };
200
+ ```
201
+
202
+ ## API documentation
203
+
204
+ The API documentation can be found at
205
+ [https://api.mittwald.de/v2/docs/](https://api.mittwald.de/v2/docs/). You can
206
+ find the operation ID on the right side of each operation.
207
+
208
+ ## Accessing the underlying Axios instance
209
+
210
+ The client uses the popular [Axios HTTP client](https://axios-http.com) under
211
+ the hood. You may access the Axios instance with the clients `axios` property.
212
+
213
+ ### Adding custom HTTP headers
214
+
215
+ To add custom HTTP headers to all requests you can use Axios' `defaults.headers`
216
+ property:
217
+
218
+ ```typescript
219
+ client.axios.defaults.headers["User-Agent"] = `your-tool/v1.2.3`;
220
+ ```
221
+
222
+ ### Intercepting requests or responses
223
+
224
+ To intercept requests or responses you can use
225
+ [Axios' interceptors](https://axios-http.com/docs/interceptors).
226
+
227
+ [pkg-auth]:
228
+ https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry#authenticating-to-github-packages
229
+ [api-getting-started]: https://developer.mittwald.de/docs/v2/api/intro
230
+
231
+ ## Usage with TypeScript
232
+
233
+ All response and request types can be imported from the `MittwaldAPIV2`
234
+ namespace.
235
+
236
+ ### Referencing request/response types
237
+
238
+ ```typescript
239
+ import { MittwaldAPIV2 } from "@mittwald/api-client";
240
+
241
+ type ProjectData = MittwaldAPIV2.Operations.ProjectGetProject.ResponseData;
242
+
243
+ // Reference "non-200" response type
244
+ type ProjectNotFoundData =
245
+ MittwaldAPIV2.Operations.ProjectGetProject.ResponseData<404>;
246
+
247
+ type CreateProjectData =
248
+ MittwaldAPIV2.Operations.ProjectCreateProject.RequestData;
249
+ ```
250
+
251
+ ## Migrating from package version V2 to V3
252
+
253
+ **This instruction considers migrating from _package_ version V2 to V3, which
254
+ has a breaking change in the call signature of request calls. The API itself has
255
+ not changed and is still at version 2.**
256
+
257
+ Path parameters are a primary component of the request and thus should not be
258
+ "hidden" in the request config object. In V3 the API of the request
259
+ configuration object has changed, and you have to set the path parameters
260
+ directly in the root level of the request object.
261
+
262
+ ```javascript
263
+ // V2
264
+ mittwaldApi.project.getProject({
265
+ pathParameters: {
266
+ projectId: "p-xxxxx",
267
+ },
268
+ });
269
+
270
+ // V3
271
+ mittwaldApi.project.getProject({
272
+ projectId: "p-xxxxx",
273
+ });
274
+ ```
package/package.json ADDED
@@ -0,0 +1,83 @@
1
+ {
2
+ "name": "@mittwald/api-client",
3
+ "version": "0.0.0-development-04b7288-20240610",
4
+ "author": "Mittwald CM Service GmbH & Co. KG <opensource@mittwald.de>",
5
+ "type": "module",
6
+ "description": "Auto-generated client for the mittwald API",
7
+ "keywords": [
8
+ "api",
9
+ "client",
10
+ "mittwald",
11
+ "rest",
12
+ "sdk"
13
+ ],
14
+ "homepage": "https://developer.mittwald.de",
15
+ "repository": "github:mittwald/api-client-js",
16
+ "bugs": {
17
+ "url": "https://github.com/mittwald/api-client-js/issues"
18
+ },
19
+ "license": "MIT",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/types/index.d.ts",
23
+ "import": "./dist/esm/index.js"
24
+ },
25
+ "./react": {
26
+ "types": "./dist/types/react.d.ts",
27
+ "import": "./dist/esm/react.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "scripts": {
34
+ "build": "run build:clean && run tsc",
35
+ "build:clean": "run rimraf dist",
36
+ "build:client-base": "run acg generate --name MittwaldAPIV2 /dev/stdin src/generated/v2 --optionalHeader x-access-token",
37
+ "build:client-dev": "bash -e -o pipefail -c 'spec/transform-openapi.sh < spec/openapi-dev.json | run build:client-base'",
38
+ "build:client-prod": "bash -e -o pipefail -c 'spec/transform-openapi.sh < spec/openapi.json | run build:client-base'",
39
+ "build:fetch-openapi": "curl --location --fail --silent https://api.mittwald.de/v2/openapi.json > spec/openapi.json",
40
+ "build:fetch-openapi-dev": "curl --location --fail --silent https://api-public.dev.mittwald.systems/v2/openapi.json > spec/openapi-dev.json",
41
+ "build:write-version-file": "run tsx dev/writeVersion.ts",
42
+ "format": "run prettier --write '**/*.{ts,tsx,yaml,yml,json,md,mdx,js}'",
43
+ "lint": "run eslint .",
44
+ "test": "",
45
+ "test:client-generation-clean": "git diff --exit-code"
46
+ },
47
+ "dependencies": {
48
+ "@mittwald/api-client-commons": "^0.0.0-development-04b7288-20240610",
49
+ "browser-or-node": "^3.0.0-pre.0"
50
+ },
51
+ "devDependencies": {
52
+ "@mittwald/api-code-generator": "^0.0.0-development-04b7288-20240610",
53
+ "@mittwald/react-use-promise": "^2.3.12",
54
+ "@types/node": "^20.11.25",
55
+ "@types/react": "^18.2.64",
56
+ "@typescript-eslint/eslint-plugin": "^7.1.1",
57
+ "@typescript-eslint/parser": "^7.1.1",
58
+ "concurrently": "^8.2.2",
59
+ "eslint": "^8.57.0",
60
+ "eslint-config-prettier": "^9.1.0",
61
+ "eslint-plugin-json": "^3.1.0",
62
+ "eslint-plugin-prettier": "^5.1.3",
63
+ "has-flag": "^5.0.1",
64
+ "prettier": "^3.2.5",
65
+ "prettier-plugin-jsdoc": "^1.3.0",
66
+ "prettier-plugin-pkgsort": "^0.2.1",
67
+ "prettier-plugin-sort-json": "^3.1.0",
68
+ "react": "^18.2.0",
69
+ "read-pkg": "^9.0.1",
70
+ "rimraf": "^5.0.5",
71
+ "tsx": "^4.7.1",
72
+ "typescript": "5.4.2"
73
+ },
74
+ "peerDependencies": {
75
+ "@mittwald/react-use-promise": "^2.3.12"
76
+ },
77
+ "peerDependenciesMeta": {
78
+ "@mittwald/react-use-promise": {
79
+ "optional": true
80
+ }
81
+ },
82
+ "gitHead": "97660b1fee614cc23b778573ba3e8ae75a961517"
83
+ }