@mittwald/api-client-commons 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 +89 -0
  3. package/package.json +82 -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,89 @@
1
+ Common code base used by `@mittwald/api-client-*` package.
2
+
3
+ ## API
4
+
5
+ ### assertStatus, assertOneOfStatus
6
+
7
+ The API client does not validate any response status by design, to give you the
8
+ most flexibility while handling also erroneous responses. If you want to assert
9
+ some desired response status, you can use the `assertStatus` resp.
10
+ `assertOneOfStatus` function.
11
+
12
+ #### assertStatus(response, expectedStatus)
13
+
14
+ Returns: void
15
+
16
+ This method throws an `ApiClientError` if the given `response` does not match
17
+ the `expectedStatus`.
18
+
19
+ When you are using TypeScript this function also asserts the correct response
20
+ type.
21
+
22
+ ```ts
23
+ const response = await client.project.getProject({
24
+ pathParameters: {
25
+ projectId: "...",
26
+ },
27
+ });
28
+
29
+ assertStatus(response, 200);
30
+
31
+ const project = response.data;
32
+ // Project properties can now be accessed safely
33
+ const name = project.name;
34
+ ```
35
+
36
+ #### assertOnOfStatus(response, expectedStatus)
37
+
38
+ Returns: void
39
+
40
+ This method throws an `ApiClientError` if the given `response` does not match
41
+ the `expectedStatus`.
42
+
43
+ When you are using TypeScript this function also asserts the correct response
44
+ type.
45
+
46
+ ```ts
47
+ const response = await client.project.getProject({
48
+ pathParameters: {
49
+ projectId: "...",
50
+ },
51
+ });
52
+
53
+ assertOneOfStatus(response, [200, 404]);
54
+
55
+ if (!response.data) {
56
+ console.log("Project not found");
57
+ }
58
+ ```
59
+
60
+ ### Common interceptors
61
+
62
+ To make it easy to opt in to some API mechanisms, a few
63
+ [interceptors](https://axios-http.com/docs/interceptors) can be easily
64
+ configured.
65
+
66
+ #### withAccessToken
67
+
68
+ In most cases you want to add the users access token to every API-Request as a
69
+ request header. This can be tedious if you need to do this on your own. To
70
+ automatically set the request header use `withAccessToken` for your `APIClient`
71
+ instance.
72
+
73
+ ```ts
74
+ const client = new APIClient({ baseURL });
75
+ const authenticatedClient = withToken(client, token);
76
+ ```
77
+
78
+ #### withEventConsistencyHandling
79
+
80
+ To opt in into the
81
+ [event consistency handling](https://developer.mittwald.de/docs/v2/api/intro/#eventual-consistency)
82
+ you might use `withEventConsistencyHandling`. This will set automatically handle
83
+ the `etag` response header and set its value as `if-event-reached` request
84
+ header for GET requests:
85
+
86
+ ```ts
87
+ const client = new APIClient({ baseURL });
88
+ const authenticatedClient = withEventConsistencyHandling(client);
89
+ ```
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@mittwald/api-client-commons",
3
+ "version": "0.0.0-development-04b7288-20240610",
4
+ "author": "Mittwald CM Service GmbH & Co. KG <opensource@mittwald.de>",
5
+ "type": "module",
6
+ "description": "Common types and utilities for mittwald API clients",
7
+ "keywords": [
8
+ "api",
9
+ "client",
10
+ "mittwald",
11
+ "rest",
12
+ "sdk"
13
+ ],
14
+ "homepage": "https://developer.mittwald.de",
15
+ "repository": "https://github.com/mittwald/api-client-js.git",
16
+ "bugs": "https://github.com/mittwald/api-client-js/issues",
17
+ "license": "MIT",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/types/index.d.ts",
21
+ "default": "./dist/esm/index.js"
22
+ },
23
+ "./testing": {
24
+ "types": "./dist/types/testing/index.d.ts",
25
+ "default": "./dist/esm/testing/index.js"
26
+ },
27
+ "./react": {
28
+ "types": "./dist/types/react/index.d.ts",
29
+ "default": "./dist/esm/react/index.js"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "scripts": {
36
+ "build": "run build:clean && run tsc",
37
+ "build:clean": "run rimraf dist",
38
+ "format": "run prettier --write '**/*.{ts,tsx,yaml,yml,json,md,mdx,js}'",
39
+ "lint": "run eslint .",
40
+ "test": "node --experimental-vm-modules $(yarn bin jest)"
41
+ },
42
+ "dependencies": {
43
+ "@types/parse-path": "^7.0.3",
44
+ "axios": "^1.6.7",
45
+ "parse-path": "^7.0.0",
46
+ "path-to-regexp": "^6.2.1",
47
+ "type-fest": "^4.12.0"
48
+ },
49
+ "devDependencies": {
50
+ "@jest/globals": "^29.7.0",
51
+ "@mittwald/react-use-promise": "^2.3.12",
52
+ "@types/jest": "^29.5.12",
53
+ "@typescript-eslint/eslint-plugin": "^7.1.1",
54
+ "@typescript-eslint/parser": "^7.1.1",
55
+ "eslint": "^8.57.0",
56
+ "eslint-config-prettier": "^9.1.0",
57
+ "eslint-plugin-json": "^3.1.0",
58
+ "eslint-plugin-prettier": "^5.1.3",
59
+ "jest": "^29.7.0",
60
+ "prettier": "^3.2.5",
61
+ "prettier-plugin-jsdoc": "^1.3.0",
62
+ "prettier-plugin-pkgsort": "^0.2.1",
63
+ "prettier-plugin-sort-json": "^3.1.0",
64
+ "react": "^18.2.0",
65
+ "rimraf": "^5.0.5",
66
+ "ts-jest": "^29.1.2",
67
+ "tsd": "^0.30.7",
68
+ "typescript": "^5.4.2"
69
+ },
70
+ "peerDependencies": {
71
+ "@mittwald/react-use-promise": "^2.1.0"
72
+ },
73
+ "peerDependenciesMeta": {
74
+ "@jest/globals": {
75
+ "optional": true
76
+ },
77
+ "@mittwald/react-use-promise": {
78
+ "optional": true
79
+ }
80
+ },
81
+ "gitHead": "97660b1fee614cc23b778573ba3e8ae75a961517"
82
+ }