@mxpicture/gcp-functions-backend 0.1.28 → 0.1.30

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.
@@ -1,14 +1,20 @@
1
1
  import type { MetaItem } from "@mxpicture/zod-meta";
2
- import type { ApiKey, ApiFilter, DocumentData, WithKey } from "@mxpicture/gcp-functions-common/types";
2
+ import type { ApiKey, ApiFilter, DocumentData, WithKey, CallbackAble } from "@mxpicture/gcp-functions-common/types";
3
3
  import { IApi } from "@mxpicture/gcp-functions-common/api";
4
4
  import { Store } from "../store/Store.js";
5
5
  import type { Validation } from "../validation/Validation.js";
6
6
  import type { ZodRawShape } from "zod";
7
7
  export declare class BackendApi<DTO extends DocumentData, STORE extends Store<DTO> = Store<DTO>, VAL extends Validation<DTO> = Validation<DTO>> extends IApi<DTO> {
8
- readonly store: STORE;
9
- readonly validation: VAL;
10
- protected shape: ZodRawShape;
11
- constructor(store: STORE, validation: VAL, shape: ZodRawShape);
8
+ protected readonly storeC: CallbackAble<STORE>;
9
+ protected readonly validationC: CallbackAble<VAL>;
10
+ protected readonly shapeC: CallbackAble<ZodRawShape>;
11
+ protected _store?: STORE;
12
+ protected _validation?: VAL;
13
+ protected _shape?: ZodRawShape;
14
+ constructor(storeC: CallbackAble<STORE>, validationC: CallbackAble<VAL>, shapeC: CallbackAble<ZodRawShape>);
15
+ protected store(): Promise<STORE>;
16
+ protected validation(): Promise<VAL>;
17
+ protected shape(): Promise<ZodRawShape>;
12
18
  delete(p: ApiKey): Promise<void>;
13
19
  get(p: ApiKey): Promise<WithKey<DTO>>;
14
20
  query(payload?: ApiFilter): Promise<WithKey<DTO>[]>;
@@ -1,39 +1,58 @@
1
1
  import { Meta } from "@mxpicture/zod-meta";
2
2
  import { IApi } from "@mxpicture/gcp-functions-common/api";
3
+ import { callableUnwrap } from "@mxpicture/gcp-functions-common/helper";
3
4
  export class BackendApi extends IApi {
4
- store;
5
- validation;
6
- shape;
7
- constructor(store, validation, shape) {
5
+ storeC;
6
+ validationC;
7
+ shapeC;
8
+ _store;
9
+ _validation;
10
+ _shape;
11
+ constructor(storeC, validationC, shapeC) {
8
12
  super();
9
- this.store = store;
10
- this.validation = validation;
11
- this.shape = shape;
13
+ this.storeC = storeC;
14
+ this.validationC = validationC;
15
+ this.shapeC = shapeC;
16
+ }
17
+ async store() {
18
+ if (!this._store)
19
+ this._store = await callableUnwrap(this.storeC);
20
+ return this._store;
21
+ }
22
+ async validation() {
23
+ if (!this._validation)
24
+ this._validation = await callableUnwrap(this.validationC);
25
+ return this._validation;
26
+ }
27
+ async shape() {
28
+ if (!this._shape)
29
+ this._shape = await callableUnwrap(this.shapeC);
30
+ return this._shape;
12
31
  }
13
32
  async delete(p) {
14
- return this.store.delete(p.id);
33
+ return (await this.store()).delete(p.id);
15
34
  }
16
35
  async get(p) {
17
- return this.store.get(p.id);
36
+ return (await this.store()).get(p.id);
18
37
  }
19
38
  async query(payload) {
20
- return this.store.query(payload?.filters);
39
+ return (await this.store()).query(payload?.filters);
21
40
  }
22
41
  async count(payload) {
23
- return { count: await this.store.count(payload?.filters) };
42
+ return { count: await (await this.store()).count(payload?.filters) };
24
43
  }
25
44
  async exists(p) {
26
- return { exists: await this.store.exists(p.id) };
45
+ return { exists: await (await this.store()).exists(p.id) };
27
46
  }
28
47
  async meta() {
29
- return new Meta(this.shape).items();
48
+ return new Meta(await this.shape()).items();
30
49
  }
31
50
  async create(data) {
32
- const result = await this.validation.validate(data);
33
- return this.store.create(result);
51
+ const result = await (await this.validation()).validate(data);
52
+ return (await this.store()).create(result);
34
53
  }
35
54
  async update(p, data) {
36
- const result = await this.validation.validatePartial(data);
37
- return this.store.update(p.id, result);
55
+ const result = await (await this.validation()).validatePartial(data);
56
+ return (await this.store()).update(p.id, result);
38
57
  }
39
58
  }
@@ -1,9 +1,11 @@
1
1
  import { HttpsFunction } from "firebase-functions/https";
2
- import type { HttpHandlers, DocumentData, ApiRoutes } from "@mxpicture/gcp-functions-common/types";
2
+ import type { HttpHandlers, DocumentData, ApiRoutes, CallbackAble } from "@mxpicture/gcp-functions-common/types";
3
3
  export declare class BackendFunction<DTO extends DocumentData, HANDLER extends HttpHandlers<ApiRoutes<DTO>> = HttpHandlers<ApiRoutes<DTO>>> {
4
4
  readonly name: string;
5
- protected http: HANDLER;
5
+ protected httpC: CallbackAble<HANDLER>;
6
6
  protected readonly httpMethods: string[];
7
- constructor(name: string, http: HANDLER);
7
+ protected _http?: HANDLER;
8
+ constructor(name: string, httpC: CallbackAble<HANDLER>);
9
+ protected http(): Promise<HANDLER>;
8
10
  createFunction(): HttpsFunction;
9
11
  }
@@ -1,14 +1,21 @@
1
1
  import { HttpsError, onCall } from "firebase-functions/https";
2
2
  import { API_ROUTES_METHODS } from "@mxpicture/gcp-functions-common/config";
3
+ import { callableUnwrap } from "@mxpicture/gcp-functions-common/helper";
3
4
  export class BackendFunction {
4
5
  name;
5
- http;
6
+ httpC;
6
7
  httpMethods;
7
- constructor(name, http) {
8
+ _http;
9
+ constructor(name, httpC) {
8
10
  this.name = name;
9
- this.http = http;
11
+ this.httpC = httpC;
10
12
  this.httpMethods = API_ROUTES_METHODS;
11
13
  }
14
+ async http() {
15
+ if (!this._http)
16
+ this._http = await callableUnwrap(this.httpC);
17
+ return this._http;
18
+ }
12
19
  createFunction() {
13
20
  return onCall({
14
21
  cors: [
@@ -26,7 +33,7 @@ export class BackendFunction {
26
33
  throw new HttpsError("invalid-argument", `Route ${req.data.route} not available`);
27
34
  return {
28
35
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
- doc: await this.http[req.data.route](req.data),
36
+ doc: await (await this.http())[req.data.route](req.data),
30
37
  };
31
38
  });
32
39
  }
@@ -1,8 +1,10 @@
1
- import type { HttpHandlers, FunctionPayload, ApiFilter, ApiKey, ApiRoutes, DocumentData } from "@mxpicture/gcp-functions-common/types";
1
+ import type { HttpHandlers, FunctionPayload, ApiFilter, ApiKey, ApiRoutes, DocumentData, CallbackAble } from "@mxpicture/gcp-functions-common/types";
2
2
  import { BackendApi } from "../api/BackendApi.js";
3
3
  export declare class BackendHttp<DTO extends DocumentData, API extends BackendApi<DTO> = BackendApi<DTO>> implements HttpHandlers<ApiRoutes<DTO>> {
4
- protected api: API;
5
- constructor(api: API);
4
+ protected apiC: CallbackAble<API>;
5
+ protected _api?: API;
6
+ constructor(apiC: CallbackAble<API>);
7
+ protected api(): Promise<API>;
6
8
  delete(req: FunctionPayload<never, ApiKey>): Promise<void>;
7
9
  get(req: FunctionPayload<never, ApiKey>): Promise<import("@mxpicture/gcp-functions-common/types").WithKey<DTO>>;
8
10
  query(req: FunctionPayload<ApiFilter>): Promise<import("@mxpicture/gcp-functions-common/types").WithKey<DTO>[]>;
@@ -1,15 +1,22 @@
1
1
  import { HttpsError } from "firebase-functions/https";
2
+ import { callableUnwrap } from "@mxpicture/gcp-functions-common/helper";
2
3
  export class BackendHttp {
3
- api;
4
- constructor(api) {
5
- this.api = api;
4
+ apiC;
5
+ _api;
6
+ constructor(apiC) {
7
+ this.apiC = apiC;
8
+ }
9
+ async api() {
10
+ if (!this._api)
11
+ this._api = await callableUnwrap(this.apiC);
12
+ return this._api;
6
13
  }
7
14
  async delete(req) {
8
15
  try {
9
16
  const id = req.keys?.id;
10
17
  if (!id)
11
18
  throw new HttpsError("invalid-argument", "Id invalid");
12
- await this.api.delete({ id });
19
+ await (await this.api()).delete({ id });
13
20
  }
14
21
  catch (error) {
15
22
  if (error instanceof HttpsError)
@@ -22,7 +29,7 @@ export class BackendHttp {
22
29
  const id = req.keys?.id;
23
30
  if (!id)
24
31
  throw new HttpsError("invalid-argument", "Id invalid");
25
- return this.api.get({ id });
32
+ return (await this.api()).get({ id });
26
33
  }
27
34
  catch (error) {
28
35
  if (error instanceof HttpsError)
@@ -32,7 +39,7 @@ export class BackendHttp {
32
39
  }
33
40
  async query(req) {
34
41
  try {
35
- return this.api.query(req.content);
42
+ return (await this.api()).query(req.content);
36
43
  }
37
44
  catch (error) {
38
45
  if (error instanceof HttpsError)
@@ -42,7 +49,7 @@ export class BackendHttp {
42
49
  }
43
50
  async count(req) {
44
51
  try {
45
- return this.api.count(req.content);
52
+ return (await this.api()).count(req.content);
46
53
  }
47
54
  catch (error) {
48
55
  if (error instanceof HttpsError)
@@ -55,7 +62,7 @@ export class BackendHttp {
55
62
  const id = req.keys?.id;
56
63
  if (!id)
57
64
  throw new HttpsError("invalid-argument", "Id invalid");
58
- return await this.api.exists({ id });
65
+ return await (await this.api()).exists({ id });
59
66
  }
60
67
  catch (error) {
61
68
  if (error instanceof HttpsError)
@@ -65,7 +72,7 @@ export class BackendHttp {
65
72
  }
66
73
  async meta() {
67
74
  try {
68
- return this.api.meta();
75
+ return (await this.api()).meta();
69
76
  }
70
77
  catch (error) {
71
78
  if (error instanceof HttpsError)
@@ -77,7 +84,7 @@ export class BackendHttp {
77
84
  try {
78
85
  if (!req.content)
79
86
  throw new HttpsError("invalid-argument", "Content is invalid");
80
- return this.api.create(req.content);
87
+ return (await this.api()).create(req.content);
81
88
  }
82
89
  catch (error) {
83
90
  if (error instanceof HttpsError)
@@ -92,7 +99,7 @@ export class BackendHttp {
92
99
  throw new HttpsError("invalid-argument", "Id invalid");
93
100
  if (!req.content)
94
101
  throw new HttpsError("invalid-argument", "Content is invalid");
95
- return this.api.update({ id }, req.content);
102
+ return (await this.api()).update({ id }, req.content);
96
103
  }
97
104
  catch (error) {
98
105
  if (error instanceof HttpsError)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mxpicture/gcp-functions-backend",
3
- "version": "0.1.28",
3
+ "version": "0.1.30",
4
4
  "description": "Utils for google cloud functions, publishing both CommonJS and ESM builds",
5
5
  "type": "module",
6
6
  "author": "MXPicture",