1 | import { Observable } from 'rxjs';
|
2 | import { map, take } from 'rxjs/operators';
|
3 | import { CRUD } from './CRUD';
|
4 | import { MasterResourceAdapter } from './MasterResourceAdapter';
|
5 | import { Plugin } from './Plugin';
|
6 | import {
|
7 | BaseResource,
|
8 | BaseResourceOptions,
|
9 | Binary,
|
10 | Client,
|
11 | DescribeTable,
|
12 | ListTable,
|
13 | PluginManager,
|
14 | PortAllocator,
|
15 | } from './types';
|
16 |
|
17 | export interface ResourceTypeOptions {
|
18 | readonly plugin: Plugin;
|
19 | readonly name: string;
|
20 | readonly names: {
|
21 | readonly capital: string;
|
22 | readonly capitalPlural: string;
|
23 | readonly lower: string;
|
24 | readonly lowerPlural: string;
|
25 | };
|
26 | }
|
27 |
|
28 | export interface MasterResourceAdapterOptions {
|
29 | readonly pluginManager: PluginManager;
|
30 | readonly dataPath: string;
|
31 | readonly binary: Binary;
|
32 | readonly portAllocator: PortAllocator;
|
33 | }
|
34 |
|
35 | export interface GetResource$Options<ResourceOptions extends BaseResourceOptions> {
|
36 | readonly name: string;
|
37 | readonly client: Client;
|
38 | readonly options: ResourceOptions;
|
39 | }
|
40 |
|
41 | export class ResourceType<
|
42 | Resource extends BaseResource = BaseResource,
|
43 | ResourceOptions extends BaseResourceOptions = BaseResourceOptions
|
44 | > {
|
45 | public readonly plugin: Plugin;
|
46 | public readonly name: string;
|
47 | public readonly names: {
|
48 | readonly capital: string;
|
49 | readonly capitalPlural: string;
|
50 | readonly lower: string;
|
51 | readonly lowerPlural: string;
|
52 | };
|
53 |
|
54 | public constructor({ plugin, name, names }: ResourceTypeOptions) {
|
55 | this.plugin = plugin;
|
56 | this.name = name;
|
57 | this.names = names;
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 | public filterResources(resources: ReadonlyArray<Resource>, _options: ResourceOptions): ReadonlyArray<Resource> {
|
63 | return resources;
|
64 | }
|
65 |
|
66 | public getResource$({
|
67 | name,
|
68 | client,
|
69 | options,
|
70 | }: GetResource$Options<ResourceOptions>): Observable<Resource | undefined> {
|
71 | return client
|
72 | .getResource$({
|
73 | plugin: this.plugin.name,
|
74 | resourceType: this.name,
|
75 | name,
|
76 | options,
|
77 | })
|
78 | .pipe(map((resource) => resource as Resource | undefined));
|
79 | }
|
80 |
|
81 | public async getResource({
|
82 | name,
|
83 | client,
|
84 | options,
|
85 | }: GetResource$Options<ResourceOptions>): Promise<Resource | undefined> {
|
86 | return this.getResource$({
|
87 | name,
|
88 | client,
|
89 | options,
|
90 | })
|
91 | .pipe(take(1))
|
92 | .toPromise();
|
93 | }
|
94 |
|
95 | public async createMasterResourceAdapter(
|
96 | _options: MasterResourceAdapterOptions,
|
97 | ): Promise<MasterResourceAdapter<Resource, ResourceOptions>> {
|
98 | return Promise.reject(new Error('Not Implemented'));
|
99 | }
|
100 |
|
101 |
|
102 | public getCRUD(): CRUD<Resource, ResourceOptions> {
|
103 | return new CRUD({ resourceType: this });
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | public getListTable(resources: ReadonlyArray<Resource>): ListTable {
|
110 | return [['Name']].concat(resources.map((resource) => [resource.baseName]));
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 | public getDescribeTable(resource: Resource): DescribeTable {
|
116 | return [['Name', resource.baseName]];
|
117 | }
|
118 | }
|