@mittwald/api-models 0.1.0-alpha.1 → 4.10.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/LICENSE +21 -0
- package/README.md +107 -10
- package/dist/esm/app/AppInstallation/AppInstallation.js +43 -0
- package/dist/esm/app/AppInstallation/behaviors/api.js +21 -0
- package/dist/esm/app/AppInstallation/behaviors/index.js +2 -0
- package/dist/esm/app/AppInstallation/behaviors/types.js +1 -0
- package/dist/esm/app/AppInstallation/index.js +2 -0
- package/dist/esm/app/AppInstallation/types.js +1 -0
- package/dist/esm/app/index.js +1 -0
- package/dist/esm/base/DataModel.js +7 -0
- package/dist/esm/base/ReferenceModel.js +9 -0
- package/dist/esm/base/assertObjectFound.js +9 -0
- package/dist/esm/base/index.js +1 -0
- package/dist/esm/base/types.js +1 -0
- package/dist/esm/config/behaviors/api.js +40 -0
- package/dist/esm/config/behaviors/index.js +1 -0
- package/dist/esm/config/config.js +9 -0
- package/dist/esm/config/index.js +1 -0
- package/dist/esm/customer/Customer/Customer.js +43 -0
- package/dist/esm/customer/Customer/behaviors/api.js +20 -0
- package/dist/esm/customer/Customer/behaviors/index.js +2 -0
- package/dist/esm/customer/Customer/behaviors/types.js +1 -0
- package/dist/esm/customer/Customer/index.js +2 -0
- package/dist/esm/customer/Customer/types.js +1 -0
- package/dist/esm/customer/index.js +1 -0
- package/dist/esm/domain/Ingress/Ingress.js +56 -0
- package/dist/esm/domain/Ingress/behaviors/api.js +23 -0
- package/dist/esm/domain/Ingress/behaviors/index.js +2 -0
- package/dist/esm/domain/Ingress/behaviors/types.js +1 -0
- package/dist/esm/domain/Ingress/index.js +2 -0
- package/dist/esm/domain/Ingress/types.js +1 -0
- package/dist/esm/domain/IngressPath/IngressPath.js +15 -0
- package/dist/esm/domain/IngressPath/IngressPath.test.js +16 -0
- package/dist/esm/domain/IngressPath/index.js +2 -0
- package/dist/esm/domain/IngressPath/types.js +1 -0
- package/dist/esm/domain/IngressTarget/IngressTarget.js +51 -0
- package/dist/esm/domain/IngressTarget/IngressTarget.test-types.js +13 -0
- package/dist/esm/domain/IngressTarget/index.js +2 -0
- package/dist/esm/domain/IngressTarget/types.js +1 -0
- package/dist/esm/domain/index.js +1 -0
- package/dist/esm/errors/ObjectNotFoundError.js +7 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/lib/deepFreeze.js +3 -0
- package/dist/esm/lib/provideReact.js +5 -0
- package/dist/esm/lib/types.js +1 -0
- package/dist/esm/project/Project/Project.js +69 -0
- package/dist/esm/project/Project/behaviors/api.js +51 -0
- package/dist/esm/project/Project/behaviors/inMem.js +23 -0
- package/dist/esm/project/Project/behaviors/index.js +3 -0
- package/dist/esm/project/Project/behaviors/types.js +1 -0
- package/dist/esm/project/Project/index.js +2 -0
- package/dist/esm/project/Project/types.js +1 -0
- package/dist/esm/project/index.js +1 -0
- package/dist/esm/react/MittwaldApiModelProvider.js +12 -0
- package/dist/esm/react/index.js +3 -0
- package/dist/esm/react/reactUsePromise.js +17 -0
- package/dist/esm/react.js +1 -0
- package/dist/esm/server/Server/Server.js +47 -0
- package/dist/esm/server/Server/behaviors/api.js +19 -0
- package/dist/esm/server/Server/behaviors/index.js +2 -0
- package/dist/esm/server/Server/behaviors/types.js +1 -0
- package/dist/esm/server/Server/index.js +1 -0
- package/dist/esm/server/Server/types.js +1 -0
- package/dist/esm/server/index.js +1 -0
- package/dist/types/app/AppInstallation/AppInstallation.d.ts +109 -0
- package/dist/types/app/AppInstallation/behaviors/api.d.ts +3 -0
- package/dist/types/app/AppInstallation/behaviors/index.d.ts +2 -0
- package/dist/types/app/AppInstallation/behaviors/types.d.ts +5 -0
- package/dist/types/app/AppInstallation/index.d.ts +2 -0
- package/dist/types/app/AppInstallation/types.d.ts +4 -0
- package/dist/types/app/index.d.ts +1 -0
- package/dist/types/base/DataModel.d.ts +5 -0
- package/dist/types/base/ReferenceModel.d.ts +5 -0
- package/dist/types/base/assertObjectFound.d.ts +3 -0
- package/dist/types/base/index.d.ts +1 -0
- package/dist/types/base/types.d.ts +2 -0
- package/dist/types/config/behaviors/api.d.ts +12 -0
- package/dist/types/config/behaviors/index.d.ts +1 -0
- package/dist/types/config/config.d.ts +16 -0
- package/dist/types/config/index.d.ts +1 -0
- package/dist/types/customer/Customer/Customer.d.ts +113 -0
- package/dist/types/customer/Customer/behaviors/api.d.ts +3 -0
- package/dist/types/customer/Customer/behaviors/index.d.ts +2 -0
- package/dist/types/customer/Customer/behaviors/types.d.ts +5 -0
- package/dist/types/customer/Customer/index.d.ts +2 -0
- package/dist/types/customer/Customer/types.d.ts +4 -0
- package/dist/types/customer/index.d.ts +1 -0
- package/dist/types/domain/Ingress/Ingress.d.ts +102 -0
- package/dist/types/domain/Ingress/behaviors/api.d.ts +3 -0
- package/dist/types/domain/Ingress/behaviors/index.d.ts +2 -0
- package/dist/types/domain/Ingress/behaviors/types.d.ts +5 -0
- package/dist/types/domain/Ingress/index.d.ts +2 -0
- package/dist/types/domain/Ingress/types.d.ts +6 -0
- package/dist/types/domain/IngressPath/IngressPath.d.ts +11 -0
- package/dist/types/domain/IngressPath/IngressPath.test.d.ts +1 -0
- package/dist/types/domain/IngressPath/index.d.ts +2 -0
- package/dist/types/domain/IngressPath/types.d.ts +2 -0
- package/dist/types/domain/IngressTarget/IngressTarget.d.ts +29 -0
- package/dist/types/domain/IngressTarget/IngressTarget.test-types.d.ts +1 -0
- package/dist/types/domain/IngressTarget/index.d.ts +2 -0
- package/dist/types/domain/IngressTarget/types.d.ts +6 -0
- package/dist/types/domain/index.d.ts +1 -0
- package/dist/types/errors/ObjectNotFoundError.d.ts +3 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/lib/deepFreeze.d.ts +5 -0
- package/dist/types/lib/provideReact.d.ts +11 -0
- package/dist/types/lib/types.d.ts +2 -0
- package/dist/types/project/Project/Project.d.ts +230 -0
- package/dist/types/project/Project/behaviors/api.d.ts +3 -0
- package/dist/types/project/Project/behaviors/inMem.d.ts +3 -0
- package/dist/types/project/Project/behaviors/index.d.ts +3 -0
- package/dist/types/project/Project/behaviors/types.d.ts +11 -0
- package/dist/types/project/Project/index.d.ts +2 -0
- package/dist/types/project/Project/types.d.ts +4 -0
- package/dist/types/project/index.d.ts +1 -0
- package/dist/types/react/MittwaldApiModelProvider.d.ts +2 -0
- package/dist/types/react/index.d.ts +4 -0
- package/dist/types/react/reactUsePromise.d.ts +1 -0
- package/dist/types/react.d.ts +1 -0
- package/dist/types/server/Server/Server.d.ts +72 -0
- package/dist/types/server/Server/behaviors/api.d.ts +3 -0
- package/dist/types/server/Server/behaviors/index.d.ts +2 -0
- package/dist/types/server/Server/behaviors/types.d.ts +5 -0
- package/dist/types/server/Server/index.d.ts +1 -0
- package/dist/types/server/Server/types.d.ts +4 -0
- package/dist/types/server/index.d.ts +1 -0
- package/package.json +24 -30
- package/dist/index.d.mts +0 -16601
- package/dist/index.d.ts +0 -16601
- package/dist/index.mjs +0 -704
- package/dist/index.mjs.map +0 -1
- package/dist/react.d.mts +0 -17
- package/dist/react.d.ts +0 -17
- package/dist/react.mjs +0 -17
- package/dist/react.mjs.map +0 -1
- package/dist/shared/api-models.54184fa2.mjs +0 -25
- package/dist/shared/api-models.54184fa2.mjs.map +0 -1
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
CHANGED
|
@@ -20,18 +20,14 @@ yarn add @mittwald/api-models
|
|
|
20
20
|
|
|
21
21
|
## Setup
|
|
22
22
|
|
|
23
|
-
You will need an
|
|
24
|
-
provided by this package.
|
|
25
|
-
|
|
23
|
+
You will need to initialize an API client in order to operate with the models
|
|
24
|
+
provided by this package. Use the `api` global instance for initialization with
|
|
25
|
+
some methods.
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
|
-
import {
|
|
29
|
-
import { MittwaldAPIV2Client } from "@mittwald/api-client";
|
|
28
|
+
import { api } from "@mittwald/api-models";
|
|
30
29
|
|
|
31
|
-
|
|
32
|
-
// client initialization: https://www.npmjs.com/package/@mittwald/api-client
|
|
33
|
-
const client = MittwaldAPIClient.newUnauthenticated();
|
|
34
|
-
setupApiBehaviors(client);
|
|
30
|
+
api.setupWithApiToken(process.env.MW_API_TOKEN);
|
|
35
31
|
```
|
|
36
32
|
|
|
37
33
|
## Examples
|
|
@@ -291,10 +287,111 @@ class ProjectDetailed {
|
|
|
291
287
|
```typescript
|
|
292
288
|
class ProjectDetailed {
|
|
293
289
|
public static async find(id: string): Promise<ProjectDetailed | undefined> {
|
|
294
|
-
const data = await
|
|
290
|
+
const data = await config.project.behaviors.find(id);
|
|
295
291
|
if (data !== undefined) {
|
|
296
292
|
return new Project(data.id, data);
|
|
297
293
|
}
|
|
298
294
|
}
|
|
299
295
|
}
|
|
300
296
|
```
|
|
297
|
+
|
|
298
|
+
#### How-to implement behaviors
|
|
299
|
+
|
|
300
|
+
Place a `behaviors` folder inside the model that should look like this:
|
|
301
|
+
|
|
302
|
+
```
|
|
303
|
+
Project/
|
|
304
|
+
├─ behaviors/
|
|
305
|
+
│ ├─ index.ts
|
|
306
|
+
│ ├─ types.ts (behavior interface)
|
|
307
|
+
│ ├─ api.ts (behavior implementation)
|
|
308
|
+
│ ├─ inmem.ts (behavior implementation)
|
|
309
|
+
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
##### Define `types.ts` first
|
|
313
|
+
|
|
314
|
+
It is a good starting point to first implement the interface for the behavior.
|
|
315
|
+
The interface usually just defines methods used in the behavior. Like
|
|
316
|
+
|
|
317
|
+
```ts
|
|
318
|
+
export interface ProjectBehaviors {
|
|
319
|
+
find: (id: string) => Promise<ProjectData | undefined>;
|
|
320
|
+
updateDescription: (projectId: string, description: string) => Promise<void>;
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Then register the behavior in the global behavior configuration
|
|
325
|
+
`packages/models/src/config/config.ts`.
|
|
326
|
+
|
|
327
|
+
##### Use the behaviors in the model
|
|
328
|
+
|
|
329
|
+
If the behavior interface is defined, you can start implementing the model. You
|
|
330
|
+
can also first implement the concrete API behavior, to "proof" the behavior is
|
|
331
|
+
"working" with the real API.
|
|
332
|
+
|
|
333
|
+
```ts
|
|
334
|
+
import { config } from "../../config/config.js";
|
|
335
|
+
|
|
336
|
+
class ProjectDetailed {
|
|
337
|
+
public static async find(id: string): Promise<ProjectDetailed | undefined> {
|
|
338
|
+
const data = await config.project.behaviors.find(id);
|
|
339
|
+
if (data !== undefined) {
|
|
340
|
+
return new Project(data.id, data);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
##### Implement the API behavior
|
|
347
|
+
|
|
348
|
+
The API behavior depends on an API client. You can implement the behavior as an
|
|
349
|
+
object factory, or a simple class implementing the interface. When using the
|
|
350
|
+
object factory, you do not have to redeclare the method parameter types.
|
|
351
|
+
|
|
352
|
+
Do the implementation specific stuff, thus preparing and executing the request,
|
|
353
|
+
and finally processing the response.
|
|
354
|
+
|
|
355
|
+
```ts
|
|
356
|
+
import { ProjectBehaviors } from "./types.js";
|
|
357
|
+
import { assertStatus, MittwaldAPIV2Client } from "@mittwald/api-client";
|
|
358
|
+
|
|
359
|
+
export const apiProjectBehaviors = (
|
|
360
|
+
client: MittwaldAPIV2Client,
|
|
361
|
+
): ProjectBehaviors => ({
|
|
362
|
+
find: async (id) => {
|
|
363
|
+
const response = await client.project.getProject({
|
|
364
|
+
projectId: id,
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
if (response.status === 200) {
|
|
368
|
+
return response.data;
|
|
369
|
+
}
|
|
370
|
+
assertStatus(response, 403);
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### Prepare for React
|
|
376
|
+
|
|
377
|
+
All asynchronous methods should provide a `use`-method property. This method
|
|
378
|
+
uses
|
|
379
|
+
[@mittwald/react-use-promise](https://www.npmjs.com/package/@mittwald/react-use-promise)
|
|
380
|
+
under the hood to "resolve" the promise in the "React way".
|
|
381
|
+
|
|
382
|
+
To provide this feature to your _async_ model methods, wrap the actual method
|
|
383
|
+
with the `provideReact` enhancer.
|
|
384
|
+
|
|
385
|
+
```ts
|
|
386
|
+
class ProjectDetailed {
|
|
387
|
+
public static find = provideReact(
|
|
388
|
+
async (id: string): Promise<ProjectDetailed | undefined> => {
|
|
389
|
+
const data = await config.behaviors.project.find(id);
|
|
390
|
+
|
|
391
|
+
if (data !== undefined) {
|
|
392
|
+
return new ProjectDetailed(data);
|
|
393
|
+
}
|
|
394
|
+
},
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { config } from "../../config/config.js";
|
|
2
|
+
import { classes } from "polytype";
|
|
3
|
+
import { DataModel } from "../../base/DataModel.js";
|
|
4
|
+
import assertObjectFound from "../../base/assertObjectFound.js";
|
|
5
|
+
import { ReferenceModel } from "../../base/ReferenceModel.js";
|
|
6
|
+
import { provideReact } from "../../lib/provideReact.js";
|
|
7
|
+
export class AppInstallation extends ReferenceModel {
|
|
8
|
+
static find = provideReact(async (id) => {
|
|
9
|
+
const data = await config.behaviors.appInstallation.find(id);
|
|
10
|
+
if (data !== undefined) {
|
|
11
|
+
return new AppInstallationDetailed(data);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
static get = provideReact(async (id) => {
|
|
15
|
+
const appInstallation = await this.find(id);
|
|
16
|
+
assertObjectFound(appInstallation, this, id);
|
|
17
|
+
return appInstallation;
|
|
18
|
+
});
|
|
19
|
+
static ofId(id) {
|
|
20
|
+
return new AppInstallation(id);
|
|
21
|
+
}
|
|
22
|
+
static list = provideReact(async (projectId, query = {}) => {
|
|
23
|
+
const data = await config.behaviors.appInstallation.list(projectId, query);
|
|
24
|
+
return data.map((d) => new AppInstallationListItem(d));
|
|
25
|
+
});
|
|
26
|
+
getDetailed = provideReact(() => AppInstallation.get(this.id));
|
|
27
|
+
}
|
|
28
|
+
// Common class for future extension
|
|
29
|
+
class AppInstallationCommon extends classes((DataModel), AppInstallation) {
|
|
30
|
+
constructor(data) {
|
|
31
|
+
super([data], [data.id]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class AppInstallationDetailed extends classes(AppInstallationCommon, (DataModel)) {
|
|
35
|
+
constructor(data) {
|
|
36
|
+
super([data], [data]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export class AppInstallationListItem extends classes(AppInstallationCommon, (DataModel)) {
|
|
40
|
+
constructor(data) {
|
|
41
|
+
super([data], [data]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { assertStatus } from "@mittwald/api-client";
|
|
2
|
+
import { assertOneOfStatus } from "@mittwald/api-client";
|
|
3
|
+
export const apiAppInstallationBehaviors = (client) => ({
|
|
4
|
+
find: async (id) => {
|
|
5
|
+
const response = await client.app.getAppinstallation({
|
|
6
|
+
appInstallationId: id,
|
|
7
|
+
});
|
|
8
|
+
if (response.status === 200) {
|
|
9
|
+
return response.data;
|
|
10
|
+
}
|
|
11
|
+
assertOneOfStatus(response, [404]);
|
|
12
|
+
},
|
|
13
|
+
list: async (projectId, query) => {
|
|
14
|
+
const response = await client.app.listAppinstallations({
|
|
15
|
+
queryParameters: query,
|
|
16
|
+
projectId,
|
|
17
|
+
});
|
|
18
|
+
assertStatus(response, 200);
|
|
19
|
+
return response.data;
|
|
20
|
+
},
|
|
21
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./AppInstallation/index.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import ObjectNotFoundError from "../errors/ObjectNotFoundError.js";
|
|
2
|
+
export default function assertObjectFound(obj, theClass, refIdOrObject) {
|
|
3
|
+
if (obj === undefined) {
|
|
4
|
+
const refName = typeof refIdOrObject === "string"
|
|
5
|
+
? refIdOrObject
|
|
6
|
+
: refIdOrObject.toString();
|
|
7
|
+
throw new ObjectNotFoundError(theClass.name, refName);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { MittwaldAPIV2Client } from "@mittwald/api-client";
|
|
2
|
+
import { config } from "../config.js";
|
|
3
|
+
import { apiProjectBehaviors } from "../../project/Project/behaviors/index.js";
|
|
4
|
+
import { apiServerBehaviors } from "../../server/Server/behaviors/index.js";
|
|
5
|
+
import { apiCustomerBehaviors } from "../../customer/Customer/behaviors/index.js";
|
|
6
|
+
import { apiIngressBehaviors } from "../../domain/Ingress/behaviors/index.js";
|
|
7
|
+
import { apiAppInstallationBehaviors } from "../../app/AppInstallation/behaviors/index.js";
|
|
8
|
+
class ApiSetupState {
|
|
9
|
+
_client;
|
|
10
|
+
setupWithClient(client) {
|
|
11
|
+
if (this._client !== undefined) {
|
|
12
|
+
throw new Error("API already setup. If you want to operate on the API client use api.client.");
|
|
13
|
+
}
|
|
14
|
+
this._client = client;
|
|
15
|
+
config.behaviors.project = apiProjectBehaviors(client);
|
|
16
|
+
config.behaviors.server = apiServerBehaviors(client);
|
|
17
|
+
config.behaviors.customer = apiCustomerBehaviors(client);
|
|
18
|
+
config.behaviors.ingress = apiIngressBehaviors(client);
|
|
19
|
+
config.behaviors.appInstallation = apiAppInstallationBehaviors(client);
|
|
20
|
+
}
|
|
21
|
+
setupWithApiToken(apiToken) {
|
|
22
|
+
return this.setupWithClient(MittwaldAPIV2Client.newWithToken(apiToken));
|
|
23
|
+
}
|
|
24
|
+
setupUnauthenticated() {
|
|
25
|
+
return this.setupWithClient(MittwaldAPIV2Client.newUnauthenticated());
|
|
26
|
+
}
|
|
27
|
+
get client() {
|
|
28
|
+
if (!this._client) {
|
|
29
|
+
throw new Error("Could not get client. Behavior not initialized.");
|
|
30
|
+
}
|
|
31
|
+
return this._client;
|
|
32
|
+
}
|
|
33
|
+
get defaults() {
|
|
34
|
+
return this.client.axios.defaults;
|
|
35
|
+
}
|
|
36
|
+
get interceptors() {
|
|
37
|
+
return this.client.axios.interceptors;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export const api = new ApiSetupState();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./api.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./behaviors/index.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { config } from "../../config/config.js";
|
|
2
|
+
import { classes } from "polytype";
|
|
3
|
+
import { DataModel } from "../../base/DataModel.js";
|
|
4
|
+
import assertObjectFound from "../../base/assertObjectFound.js";
|
|
5
|
+
import { ReferenceModel } from "../../base/ReferenceModel.js";
|
|
6
|
+
import { provideReact } from "../../lib/provideReact.js";
|
|
7
|
+
export class Customer extends ReferenceModel {
|
|
8
|
+
static ofId(id) {
|
|
9
|
+
return new Customer(id);
|
|
10
|
+
}
|
|
11
|
+
static find = provideReact(async (id) => {
|
|
12
|
+
const data = await config.behaviors.customer.find(id);
|
|
13
|
+
if (data !== undefined) {
|
|
14
|
+
return new CustomerDetailed(data);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
static list = provideReact(async (query = {}) => {
|
|
18
|
+
const data = await config.behaviors.customer.list(query);
|
|
19
|
+
return Object.freeze(data.map((d) => new CustomerListItem(d)));
|
|
20
|
+
});
|
|
21
|
+
static get = provideReact(async (id) => {
|
|
22
|
+
const customer = await this.find(id);
|
|
23
|
+
assertObjectFound(customer, this, id);
|
|
24
|
+
return customer;
|
|
25
|
+
});
|
|
26
|
+
getDetailed = provideReact(() => Customer.get(this.id));
|
|
27
|
+
}
|
|
28
|
+
// Common class for future extension
|
|
29
|
+
class CustomerCommon extends classes((DataModel), Customer) {
|
|
30
|
+
constructor(data) {
|
|
31
|
+
super([data], [data.customerId]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class CustomerDetailed extends classes(CustomerCommon, (DataModel)) {
|
|
35
|
+
constructor(data) {
|
|
36
|
+
super([data], [data]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export class CustomerListItem extends classes(CustomerCommon, (DataModel)) {
|
|
40
|
+
constructor(data) {
|
|
41
|
+
super([data], [data]);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { assertStatus } from "@mittwald/api-client";
|
|
2
|
+
import { assertOneOfStatus } from "@mittwald/api-client";
|
|
3
|
+
export const apiCustomerBehaviors = (client) => ({
|
|
4
|
+
find: async (id) => {
|
|
5
|
+
const response = await client.customer.getCustomer({
|
|
6
|
+
customerId: id,
|
|
7
|
+
});
|
|
8
|
+
if (response.status === 200) {
|
|
9
|
+
return response.data;
|
|
10
|
+
}
|
|
11
|
+
assertOneOfStatus(response, [404]);
|
|
12
|
+
},
|
|
13
|
+
list: async (query) => {
|
|
14
|
+
const response = await client.customer.listCustomers({
|
|
15
|
+
queryParameters: query,
|
|
16
|
+
});
|
|
17
|
+
assertStatus(response, 200);
|
|
18
|
+
return response.data;
|
|
19
|
+
},
|
|
20
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Customer/index.js";
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { config } from "../../config/config.js";
|
|
2
|
+
import { classes } from "polytype";
|
|
3
|
+
import { DataModel } from "../../base/DataModel.js";
|
|
4
|
+
import assertObjectFound from "../../base/assertObjectFound.js";
|
|
5
|
+
import { ReferenceModel } from "../../base/ReferenceModel.js";
|
|
6
|
+
import { provideReact } from "../../lib/provideReact.js";
|
|
7
|
+
import { IngressPath } from "../IngressPath/IngressPath.js";
|
|
8
|
+
export class Ingress extends ReferenceModel {
|
|
9
|
+
static ofId(id) {
|
|
10
|
+
return new Ingress(id);
|
|
11
|
+
}
|
|
12
|
+
static ofHostname(hostname) {
|
|
13
|
+
return Ingress.ofId(hostname);
|
|
14
|
+
}
|
|
15
|
+
static list = provideReact(async (query = {}) => {
|
|
16
|
+
const data = await config.behaviors.ingress.list(query);
|
|
17
|
+
return data.map((d) => new IngressListItem(d));
|
|
18
|
+
});
|
|
19
|
+
static find = provideReact(async (id) => {
|
|
20
|
+
const data = await config.behaviors.ingress.find(id);
|
|
21
|
+
if (data !== undefined) {
|
|
22
|
+
return new IngressDetailed(data);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
static get = provideReact(async (id) => {
|
|
26
|
+
const ingress = await this.find(id);
|
|
27
|
+
assertObjectFound(ingress, this, id);
|
|
28
|
+
return ingress;
|
|
29
|
+
});
|
|
30
|
+
getDetailed = provideReact(() => Ingress.get(this.id));
|
|
31
|
+
}
|
|
32
|
+
export class IngressCommon extends classes((DataModel), Ingress) {
|
|
33
|
+
baseUrl;
|
|
34
|
+
paths;
|
|
35
|
+
defaultPath;
|
|
36
|
+
constructor(data) {
|
|
37
|
+
super([data], [data.id]);
|
|
38
|
+
this.baseUrl = `https://${data.hostname}`;
|
|
39
|
+
this.paths = Object.freeze(data.paths.map((p) => new IngressPath(this, p)));
|
|
40
|
+
const defaultPath = this.paths.find((p) => p.path === "/");
|
|
41
|
+
if (defaultPath === undefined) {
|
|
42
|
+
throw new Error(`Ingress ${this.describe()} has no default path.`);
|
|
43
|
+
}
|
|
44
|
+
this.defaultPath = defaultPath;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export class IngressDetailed extends classes(IngressCommon, (DataModel)) {
|
|
48
|
+
constructor(data) {
|
|
49
|
+
super([data], [data]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export class IngressListItem extends classes(IngressCommon, (DataModel)) {
|
|
53
|
+
constructor(data) {
|
|
54
|
+
super([data], [data]);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { assertStatus } from "@mittwald/api-client";
|
|
2
|
+
import { assertOneOfStatus } from "@mittwald/api-client";
|
|
3
|
+
export const apiIngressBehaviors = (client) => ({
|
|
4
|
+
find: async (id) => {
|
|
5
|
+
const response = await client.domain.ingressGetIngress({
|
|
6
|
+
ingressId: id,
|
|
7
|
+
});
|
|
8
|
+
if (response.status === 200) {
|
|
9
|
+
return response.data;
|
|
10
|
+
}
|
|
11
|
+
assertOneOfStatus(response, [404]);
|
|
12
|
+
},
|
|
13
|
+
list: async (query = {}) => {
|
|
14
|
+
const { projectId } = query;
|
|
15
|
+
const response = await client.domain.ingressListIngresses({
|
|
16
|
+
queryParameters: {
|
|
17
|
+
projectId,
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
assertStatus(response, 200);
|
|
21
|
+
return response.data;
|
|
22
|
+
},
|
|
23
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { DataModel } from "../../base/DataModel.js";
|
|
2
|
+
import { ingressTargetFactory, } from "../IngressTarget/IngressTarget.js";
|
|
3
|
+
export class IngressPath extends DataModel {
|
|
4
|
+
ingress;
|
|
5
|
+
path;
|
|
6
|
+
url;
|
|
7
|
+
target;
|
|
8
|
+
constructor(ingress, data) {
|
|
9
|
+
super(data);
|
|
10
|
+
this.ingress = ingress;
|
|
11
|
+
this.path = data.path;
|
|
12
|
+
this.url = new URL(data.path, ingress.baseUrl);
|
|
13
|
+
this.target = ingressTargetFactory(this, data.target);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IngressPath } from "./IngressPath.js";
|
|
2
|
+
import { IngressDetailed } from "../Ingress/index.js";
|
|
3
|
+
const ingressData = {
|
|
4
|
+
id: "abc",
|
|
5
|
+
paths: [],
|
|
6
|
+
};
|
|
7
|
+
const corruptIngressTargetData = {};
|
|
8
|
+
const ingressPathData = {
|
|
9
|
+
path: "/",
|
|
10
|
+
target: corruptIngressTargetData,
|
|
11
|
+
};
|
|
12
|
+
test("Creating IngressPath with corrupt IngressTarget throws error", () => {
|
|
13
|
+
expect(() => {
|
|
14
|
+
new IngressPath(new IngressDetailed(ingressData), ingressPathData);
|
|
15
|
+
}).toThrowError("Ingress IngressDetailed@abc has no default path.");
|
|
16
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { DataModel } from "../../base/DataModel.js";
|
|
2
|
+
import { AppInstallation } from "../../app/AppInstallation/index.js";
|
|
3
|
+
class IngressTargetBase extends DataModel {
|
|
4
|
+
path;
|
|
5
|
+
constructor(path, data) {
|
|
6
|
+
super(data);
|
|
7
|
+
this.path = path;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export class IngressRedirectTarget extends IngressTargetBase {
|
|
11
|
+
type = "redirect";
|
|
12
|
+
url;
|
|
13
|
+
constructor(path, data) {
|
|
14
|
+
super(path, data);
|
|
15
|
+
this.url = new URL(data.url);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export class IngressDirectoryTarget extends IngressTargetBase {
|
|
19
|
+
type = "directory";
|
|
20
|
+
directory;
|
|
21
|
+
constructor(path, data) {
|
|
22
|
+
super(path, data);
|
|
23
|
+
this.directory = data.directory;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export class IngressAppInstallationTarget extends IngressTargetBase {
|
|
27
|
+
type = "appInstallation";
|
|
28
|
+
appInstallation;
|
|
29
|
+
constructor(path, data) {
|
|
30
|
+
super(path, data);
|
|
31
|
+
this.appInstallation = AppInstallation.ofId(data.installationId);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
export class IngressUndefinedTarget extends IngressTargetBase {
|
|
35
|
+
type = "undefined";
|
|
36
|
+
}
|
|
37
|
+
export const ingressTargetFactory = (path, data) => {
|
|
38
|
+
if ("directory" in data) {
|
|
39
|
+
return new IngressDirectoryTarget(path, data);
|
|
40
|
+
}
|
|
41
|
+
if ("url" in data) {
|
|
42
|
+
return new IngressRedirectTarget(path, data);
|
|
43
|
+
}
|
|
44
|
+
if ("installationId" in data) {
|
|
45
|
+
return new IngressAppInstallationTarget(path, data);
|
|
46
|
+
}
|
|
47
|
+
if ("useDefaultPage" in data) {
|
|
48
|
+
return new IngressUndefinedTarget(path, data);
|
|
49
|
+
}
|
|
50
|
+
throw new Error("Ingress target type is not supported.");
|
|
51
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const target = {};
|
|
2
|
+
// @ts-expect-error use type-guard first
|
|
3
|
+
void target.url;
|
|
4
|
+
if (target.type === "redirect") {
|
|
5
|
+
void target.url;
|
|
6
|
+
}
|
|
7
|
+
if (target.type === "directory") {
|
|
8
|
+
void target.directory;
|
|
9
|
+
}
|
|
10
|
+
if (target.type === "appInstallation") {
|
|
11
|
+
void target.appInstallation.id;
|
|
12
|
+
}
|
|
13
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./Ingress/index.js";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { MittwaldAPIV2Client } from "@mittwald/api-client";
|
|
2
|
+
export * from "./config/index.js";
|
|
3
|
+
export * from "./project/index.js";
|
|
4
|
+
export * from "./server/index.js";
|
|
5
|
+
export * from "./domain/index.js";
|
|
6
|
+
export * from "./customer/index.js";
|
|
7
|
+
export * from "./base/index.js";
|