@mxpicture/gcp-functions-backend 0.1.48 → 0.1.50
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/dist/api/BackendApi.d.ts +2 -4
- package/dist/api/BackendApi.js +10 -14
- package/dist/api/IBackendApi.d.ts +7 -11
- package/dist/api/IBackendApi.js +11 -18
- package/dist/function/IBackendFunction.d.ts +8 -8
- package/dist/function/IBackendFunction.js +14 -14
- package/dist/validation/Validation.d.ts +9 -8
- package/dist/validation/Validation.js +16 -14
- package/package.json +4 -4
package/dist/api/BackendApi.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { DocumentData, WithKey, CallbackAble, ApiFromRoutes, CrudRoutes, WithoutKey, DocumentKey, ApiFilter } from "@mxpicture/gcp-functions-common/types";
|
|
1
|
+
import type { DocumentData, WithKey, ApiFromRoutes, CrudRoutes, WithoutKey, DocumentKey, ApiFilter } from "@mxpicture/gcp-functions-common/types";
|
|
3
2
|
import { Store } from "../store/Store.js";
|
|
4
3
|
import type { Validation } from "../validation/Validation.js";
|
|
5
|
-
import type { ZodRawShape } from "zod";
|
|
6
4
|
import { IBackendApi } from "./IBackendApi.js";
|
|
5
|
+
import type { MetaItem } from "@mxpicture/gcp-functions-common/meta";
|
|
7
6
|
export declare class BackendApi<DTO extends DocumentData, STORE extends Store<DTO> = Store<DTO>, VAL extends Validation<DTO> = Validation<DTO>> extends IBackendApi<DTO, STORE, VAL> implements ApiFromRoutes<CrudRoutes<DTO>> {
|
|
8
|
-
constructor(name: string, storeC: CallbackAble<STORE>, validationC: CallbackAble<VAL>, shapeC: CallbackAble<ZodRawShape>);
|
|
9
7
|
delete(request: DocumentKey): Promise<{
|
|
10
8
|
deleted: true;
|
|
11
9
|
}>;
|
package/dist/api/BackendApi.js
CHANGED
|
@@ -1,34 +1,30 @@
|
|
|
1
|
-
import { Meta } from "@mxpicture/zod-meta";
|
|
2
1
|
import { IBackendApi } from "./IBackendApi.js";
|
|
3
2
|
export class BackendApi extends IBackendApi {
|
|
4
|
-
constructor(name, storeC, validationC, shapeC) {
|
|
5
|
-
super(name, storeC, validationC, shapeC);
|
|
6
|
-
}
|
|
7
3
|
async delete(request) {
|
|
8
|
-
await
|
|
4
|
+
await this.store().delete(request.id);
|
|
9
5
|
return { deleted: true };
|
|
10
6
|
}
|
|
11
7
|
async get(request) {
|
|
12
|
-
return
|
|
8
|
+
return this.store().get(request.id);
|
|
13
9
|
}
|
|
14
10
|
async query(request) {
|
|
15
|
-
return
|
|
11
|
+
return this.store().query(request?.filters);
|
|
16
12
|
}
|
|
17
13
|
async count(request) {
|
|
18
|
-
return { count: await
|
|
14
|
+
return { count: await this.store().count(request?.filters) };
|
|
19
15
|
}
|
|
20
16
|
async exists(request) {
|
|
21
|
-
return { exists: await
|
|
17
|
+
return { exists: await this.store().exists(request.id) };
|
|
22
18
|
}
|
|
23
19
|
async meta() {
|
|
24
|
-
|
|
20
|
+
throw new Error("not implemented"); // todo
|
|
25
21
|
}
|
|
26
22
|
async create(request) {
|
|
27
|
-
const result =
|
|
28
|
-
return
|
|
23
|
+
const result = this.validation().validate(request);
|
|
24
|
+
return this.store().create(result);
|
|
29
25
|
}
|
|
30
26
|
async update(request) {
|
|
31
|
-
const result =
|
|
32
|
-
return
|
|
27
|
+
const result = this.validation().validatePartial(request);
|
|
28
|
+
return this.store().update(request.id, result);
|
|
33
29
|
}
|
|
34
30
|
}
|
|
@@ -1,17 +1,13 @@
|
|
|
1
|
-
import type { DocumentData
|
|
1
|
+
import type { DocumentData } from "@mxpicture/gcp-functions-common/types";
|
|
2
2
|
import { Store } from "../store/Store.js";
|
|
3
3
|
import type { Validation } from "../validation/Validation.js";
|
|
4
|
-
|
|
5
|
-
export declare class IBackendApi<DTO extends DocumentData, STORE extends Store<DTO> = Store<DTO>, VAL extends Validation<DTO> = Validation<DTO>> {
|
|
4
|
+
export declare abstract class IBackendApi<DTO extends DocumentData, STORE extends Store<DTO> = Store<DTO>, VAL extends Validation<DTO> = Validation<DTO>> {
|
|
6
5
|
readonly name: string;
|
|
7
|
-
protected readonly storeC: CallbackAble<STORE>;
|
|
8
|
-
protected readonly validationC: CallbackAble<VAL>;
|
|
9
|
-
protected readonly shapeC: CallbackAble<ZodRawShape>;
|
|
10
6
|
protected _store?: STORE;
|
|
11
7
|
protected _validation?: VAL;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
protected
|
|
16
|
-
protected
|
|
8
|
+
constructor(name: string);
|
|
9
|
+
useStore(store: STORE): void;
|
|
10
|
+
useValidation(validation: VAL): void;
|
|
11
|
+
protected store(): STORE;
|
|
12
|
+
protected validation(): VAL;
|
|
17
13
|
}
|
package/dist/api/IBackendApi.js
CHANGED
|
@@ -1,31 +1,24 @@
|
|
|
1
|
-
import { callableUnwrap } from "@mxpicture/gcp-functions-common/helper";
|
|
2
1
|
export class IBackendApi {
|
|
3
2
|
name;
|
|
4
|
-
storeC;
|
|
5
|
-
validationC;
|
|
6
|
-
shapeC;
|
|
7
3
|
_store;
|
|
8
4
|
_validation;
|
|
9
|
-
|
|
10
|
-
constructor(name, storeC, validationC, shapeC) {
|
|
5
|
+
constructor(name) {
|
|
11
6
|
this.name = name;
|
|
12
|
-
this.storeC = storeC;
|
|
13
|
-
this.validationC = validationC;
|
|
14
|
-
this.shapeC = shapeC;
|
|
15
7
|
}
|
|
16
|
-
|
|
8
|
+
useStore(store) {
|
|
9
|
+
this._store = store;
|
|
10
|
+
}
|
|
11
|
+
useValidation(validation) {
|
|
12
|
+
this._validation = validation;
|
|
13
|
+
}
|
|
14
|
+
store() {
|
|
17
15
|
if (!this._store)
|
|
18
|
-
this.
|
|
16
|
+
throw new Error(`${this.name}: store not provided. Use "useStore" method`);
|
|
19
17
|
return this._store;
|
|
20
18
|
}
|
|
21
|
-
|
|
19
|
+
validation() {
|
|
22
20
|
if (!this._validation)
|
|
23
|
-
this.
|
|
21
|
+
throw new Error(`${this.name}: validation not provided. Use "useValidation" method`);
|
|
24
22
|
return this._validation;
|
|
25
23
|
}
|
|
26
|
-
async shape() {
|
|
27
|
-
if (!this._shape)
|
|
28
|
-
this._shape = await callableUnwrap(this.shapeC);
|
|
29
|
-
return this._shape;
|
|
30
|
-
}
|
|
31
24
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import type { DocumentData
|
|
1
|
+
import type { DocumentData } from "@mxpicture/gcp-functions-common/types";
|
|
2
2
|
import { BackendApi } from "../api/BackendApi.js";
|
|
3
3
|
import { CallableFunction } from "firebase-functions/v2/https";
|
|
4
4
|
import { FunctionRequest, FunctionResponse } from "../../../common/src/types/types.function.js";
|
|
5
|
-
export declare class IBackendFunction<DTO extends DocumentData, API extends BackendApi<DTO>> {
|
|
6
|
-
|
|
7
|
-
protected readonly routesC: CallbackAble<string[]>;
|
|
5
|
+
export declare abstract class IBackendFunction<DTO extends DocumentData, API extends BackendApi<DTO>> {
|
|
6
|
+
readonly name: string;
|
|
8
7
|
protected _api?: API;
|
|
9
8
|
protected _routes?: string[];
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
protected
|
|
9
|
+
constructor(name: string);
|
|
10
|
+
useApi(api: API): void;
|
|
11
|
+
useRoutes(routes: string[]): void;
|
|
12
|
+
protected api(): API;
|
|
13
|
+
protected routes(): string[];
|
|
14
14
|
ingress<REQ, RES extends DocumentData>(request: FunctionRequest<REQ>): Promise<FunctionResponse<RES>>;
|
|
15
15
|
buildFunction(): CallableFunction<FunctionRequest<any>, Promise<FunctionResponse<any>>>;
|
|
16
16
|
}
|
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
import { onCall, HttpsError, } from "firebase-functions/v2/https";
|
|
2
|
-
import { callableUnwrap } from "@mxpicture/gcp-functions-common/helper";
|
|
3
2
|
export class IBackendFunction {
|
|
4
|
-
|
|
5
|
-
routesC;
|
|
3
|
+
name;
|
|
6
4
|
_api;
|
|
7
5
|
_routes;
|
|
8
|
-
constructor(
|
|
9
|
-
this.
|
|
10
|
-
this.routesC = routesC;
|
|
6
|
+
constructor(name) {
|
|
7
|
+
this.name = name;
|
|
11
8
|
}
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
useApi(api) {
|
|
10
|
+
this._api = api;
|
|
14
11
|
}
|
|
15
|
-
|
|
12
|
+
useRoutes(routes) {
|
|
13
|
+
this._routes = routes;
|
|
14
|
+
}
|
|
15
|
+
api() {
|
|
16
16
|
if (!this._api)
|
|
17
|
-
this.
|
|
17
|
+
throw new Error(`${this.name}: api not provided. Use "useApi" method`);
|
|
18
18
|
return this._api;
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
routes() {
|
|
21
21
|
if (!this._routes)
|
|
22
|
-
this.
|
|
22
|
+
throw new Error(`${this.name}: routes not provided. Use "useRoutes" method`);
|
|
23
23
|
return this._routes;
|
|
24
24
|
}
|
|
25
25
|
async ingress(request) {
|
|
26
|
-
const routes =
|
|
26
|
+
const routes = this.routes();
|
|
27
27
|
if (!routes.find((m) => m === request.route))
|
|
28
28
|
throw new HttpsError("invalid-argument", `Route ${request.route} not available`);
|
|
29
29
|
try {
|
|
30
30
|
return {
|
|
31
31
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
|
-
data: await
|
|
32
|
+
data: await this.api()[request.route](request.data),
|
|
33
33
|
};
|
|
34
34
|
}
|
|
35
35
|
catch (error) {
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { type ZodError, type ZodObject, type ZodRawShape } from "zod";
|
|
2
|
-
import type {
|
|
2
|
+
import type { DocumentData, WithKey, WithoutKey } from "@mxpicture/gcp-functions-common/types";
|
|
3
3
|
import { ValidationFieldErrors } from "../types/types.validation.js";
|
|
4
4
|
export declare class Validation<DTO extends DocumentData> {
|
|
5
|
-
|
|
5
|
+
readonly name: string;
|
|
6
6
|
protected _createSchema?: ZodObject;
|
|
7
7
|
protected _updateSchema?: ZodObject;
|
|
8
8
|
protected _shape?: ZodRawShape;
|
|
9
|
-
constructor(
|
|
10
|
-
|
|
11
|
-
protected
|
|
12
|
-
protected
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
constructor(name: string);
|
|
10
|
+
useShape(shape: ZodRawShape): void;
|
|
11
|
+
protected shape(): ZodRawShape;
|
|
12
|
+
protected createSchema(): ZodObject;
|
|
13
|
+
protected updateSchema(): ZodObject;
|
|
14
|
+
validate(doc: Partial<WithoutKey<DTO>>): WithKey<DTO>;
|
|
15
|
+
validatePartial(doc: Partial<WithKey<DTO>>): Partial<WithKey<DTO>>;
|
|
15
16
|
protected issuesToFieldErrors(issues: ZodError["issues"]): ValidationFieldErrors;
|
|
16
17
|
}
|
|
@@ -1,39 +1,41 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { HttpsError } from "firebase-functions/https";
|
|
3
|
-
import { callableUnwrap } from "@mxpicture/gcp-functions-common/helper";
|
|
4
3
|
export class Validation {
|
|
5
|
-
|
|
4
|
+
name;
|
|
6
5
|
_createSchema;
|
|
7
6
|
_updateSchema;
|
|
8
7
|
_shape;
|
|
9
|
-
constructor(
|
|
10
|
-
this.
|
|
8
|
+
constructor(name) {
|
|
9
|
+
this.name = name;
|
|
11
10
|
}
|
|
12
|
-
|
|
11
|
+
useShape(shape) {
|
|
12
|
+
this._shape = shape;
|
|
13
|
+
}
|
|
14
|
+
shape() {
|
|
13
15
|
if (!this._shape)
|
|
14
|
-
this.
|
|
16
|
+
throw new Error(`${this.name}: shape not provided. Use "useShape" method`);
|
|
15
17
|
return this._shape;
|
|
16
18
|
}
|
|
17
|
-
|
|
19
|
+
createSchema() {
|
|
18
20
|
if (!this._createSchema)
|
|
19
|
-
this._createSchema = z.object(
|
|
21
|
+
this._createSchema = z.object(this.shape());
|
|
20
22
|
return this._createSchema;
|
|
21
23
|
}
|
|
22
|
-
|
|
24
|
+
updateSchema() {
|
|
23
25
|
if (!this._updateSchema)
|
|
24
|
-
this._updateSchema =
|
|
26
|
+
this._updateSchema = this.createSchema().partial();
|
|
25
27
|
return this._updateSchema;
|
|
26
28
|
}
|
|
27
|
-
|
|
28
|
-
const result =
|
|
29
|
+
validate(doc) {
|
|
30
|
+
const result = this.createSchema().safeParse(doc);
|
|
29
31
|
if (!result.success)
|
|
30
32
|
throw new HttpsError("invalid-argument", "Invalid request data", {
|
|
31
33
|
fields: this.issuesToFieldErrors(result.error.issues),
|
|
32
34
|
});
|
|
33
35
|
return result.data;
|
|
34
36
|
}
|
|
35
|
-
|
|
36
|
-
const result =
|
|
37
|
+
validatePartial(doc) {
|
|
38
|
+
const result = this.updateSchema().safeParse(doc);
|
|
37
39
|
if (!result.success)
|
|
38
40
|
throw new HttpsError("invalid-argument", "Invalid request data", {
|
|
39
41
|
fields: this.issuesToFieldErrors(result.error.issues),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mxpicture/gcp-functions-backend",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.50",
|
|
4
4
|
"description": "Utils for google cloud functions, publishing both CommonJS and ESM builds",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "MXPicture",
|
|
@@ -28,8 +28,7 @@
|
|
|
28
28
|
"clean": "rm -rf dist .tsbuildinfo tsconfig.tsbuildinfo node_modules",
|
|
29
29
|
"lint": "eslint \"src/**/*.{ts,tsx}\" --ext .ts,.tsx",
|
|
30
30
|
"update": "ncu -u && npm install",
|
|
31
|
-
"build": "tsc -b ."
|
|
32
|
-
"update-own": "npm update --save @mxpicture/zod-meta"
|
|
31
|
+
"build": "tsc -b ."
|
|
33
32
|
},
|
|
34
33
|
"publishConfig": {
|
|
35
34
|
"access": "public"
|
|
@@ -37,7 +36,6 @@
|
|
|
37
36
|
"dependencies": {
|
|
38
37
|
"@mxpicture/gcp-functions-common": "*",
|
|
39
38
|
"@mxpicture/http-status-codes": "^0.1.28",
|
|
40
|
-
"@mxpicture/zod-meta": "^0.1.12",
|
|
41
39
|
"cookie-parser": "^1.4.7",
|
|
42
40
|
"express": "^5.2.1",
|
|
43
41
|
"firebase-admin": "^13.6.0",
|
|
@@ -49,6 +47,8 @@
|
|
|
49
47
|
"@types/cookie-parser": "^1.4.10",
|
|
50
48
|
"@types/express": "^4.17.25",
|
|
51
49
|
"@types/node": "^25.2.0",
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^8.55.0",
|
|
51
|
+
"@typescript-eslint/parser": "^8.55.0",
|
|
52
52
|
"firebase-tools": "^15.5.1",
|
|
53
53
|
"typescript": "^5.9.3"
|
|
54
54
|
}
|