@mxpicture/gcp-functions-backend 0.1.16 → 0.1.17
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/{esm/base → api}/BackendApi.d.ts +4 -5
- package/dist/{esm/base → api}/BackendApi.js +7 -8
- package/dist/api/index.d.ts +1 -0
- package/dist/api/index.js +1 -0
- package/dist/{cjs/common → firebase}/firebase.backend.d.ts +0 -1
- package/dist/firebase/firebase.backend.js +4 -0
- package/dist/firebase/index.d.ts +1 -0
- package/dist/firebase/index.js +1 -0
- package/dist/{types/base → function}/BackendFunction.d.ts +1 -2
- package/dist/function/BackendFunction.js +22 -0
- package/dist/{cjs → function}/FunctionCollection.d.ts +3 -4
- package/dist/function/FunctionCollection.js +12 -0
- package/dist/function/index.d.ts +2 -0
- package/dist/function/index.js +2 -0
- package/dist/{types/base → http}/BackendHttp.d.ts +6 -7
- package/dist/http/BackendHttp.js +103 -0
- package/dist/http/index.d.ts +1 -0
- package/dist/http/index.js +1 -0
- package/dist/{cjs/loader → loader}/zod.loader.d.ts +0 -1
- package/dist/loader/zod.loader.js +6 -0
- package/dist/{cjs/store → store}/Store.d.ts +1 -2
- package/dist/{esm/store → store}/Store.js +9 -42
- package/dist/store/index.d.ts +1 -0
- package/dist/store/index.js +1 -0
- package/dist/types/index.d.ts +1 -4
- package/dist/types/index.js +1 -0
- package/dist/types/types.validation.d.ts +1 -0
- package/dist/types/types.validation.js +1 -0
- package/dist/{cjs/validation → validation}/Validation.d.ts +3 -4
- package/dist/{cjs/validation → validation}/Validation.js +12 -13
- package/dist/validation/index.d.ts +1 -0
- package/dist/validation/index.js +1 -0
- package/package.json +12 -17
- package/dist/cjs/FunctionCollection.d.ts.map +0 -1
- package/dist/cjs/FunctionCollection.js +0 -17
- package/dist/cjs/base/BackendApi.d.ts +0 -27
- package/dist/cjs/base/BackendApi.d.ts.map +0 -1
- package/dist/cjs/base/BackendApi.js +0 -40
- package/dist/cjs/base/BackendFunction.d.ts +0 -10
- package/dist/cjs/base/BackendFunction.d.ts.map +0 -1
- package/dist/cjs/base/BackendFunction.js +0 -23
- package/dist/cjs/base/BackendHttp.d.ts +0 -19
- package/dist/cjs/base/BackendHttp.d.ts.map +0 -1
- package/dist/cjs/base/BackendHttp.js +0 -106
- package/dist/cjs/common/firebase.backend.d.ts.map +0 -1
- package/dist/cjs/common/firebase.backend.js +0 -7
- package/dist/cjs/demo/User.d.ts +0 -25
- package/dist/cjs/demo/User.d.ts.map +0 -1
- package/dist/cjs/demo/User.js +0 -33
- package/dist/cjs/index.d.ts +0 -4
- package/dist/cjs/index.d.ts.map +0 -1
- package/dist/cjs/index.js +0 -42
- package/dist/cjs/lib.d.ts +0 -10
- package/dist/cjs/lib.d.ts.map +0 -1
- package/dist/cjs/lib.js +0 -23
- package/dist/cjs/loader/zod.loader.d.ts.map +0 -1
- package/dist/cjs/loader/zod.loader.js +0 -43
- package/dist/cjs/package.d.ts +0 -2
- package/dist/cjs/package.d.ts.map +0 -1
- package/dist/cjs/package.js +0 -20
- package/dist/cjs/store/Store.d.ts.map +0 -1
- package/dist/cjs/store/Store.js +0 -128
- package/dist/cjs/validation/Validation.d.ts.map +0 -1
- package/dist/esm/FunctionCollection.d.ts +0 -13
- package/dist/esm/FunctionCollection.d.ts.map +0 -1
- package/dist/esm/FunctionCollection.js +0 -17
- package/dist/esm/base/BackendApi.d.ts.map +0 -1
- package/dist/esm/base/BackendFunction.d.ts +0 -10
- package/dist/esm/base/BackendFunction.d.ts.map +0 -1
- package/dist/esm/base/BackendFunction.js +0 -23
- package/dist/esm/base/BackendHttp.d.ts +0 -19
- package/dist/esm/base/BackendHttp.d.ts.map +0 -1
- package/dist/esm/base/BackendHttp.js +0 -106
- package/dist/esm/common/firebase.backend.d.ts +0 -3
- package/dist/esm/common/firebase.backend.d.ts.map +0 -1
- package/dist/esm/common/firebase.backend.js +0 -7
- package/dist/esm/demo/User.d.ts +0 -25
- package/dist/esm/demo/User.d.ts.map +0 -1
- package/dist/esm/demo/User.js +0 -33
- package/dist/esm/index.d.ts +0 -4
- package/dist/esm/index.d.ts.map +0 -1
- package/dist/esm/index.js +0 -42
- package/dist/esm/lib.d.ts +0 -10
- package/dist/esm/lib.d.ts.map +0 -1
- package/dist/esm/lib.js +0 -23
- package/dist/esm/loader/zod.loader.d.ts +0 -2
- package/dist/esm/loader/zod.loader.d.ts.map +0 -1
- package/dist/esm/loader/zod.loader.js +0 -10
- package/dist/esm/package.d.ts +0 -2
- package/dist/esm/package.d.ts.map +0 -1
- package/dist/esm/package.js +0 -20
- package/dist/esm/store/Store.d.ts +0 -24
- package/dist/esm/store/Store.d.ts.map +0 -1
- package/dist/esm/validation/Validation.d.ts +0 -16
- package/dist/esm/validation/Validation.d.ts.map +0 -1
- package/dist/esm/validation/Validation.js +0 -43
- package/dist/tsconfig.cjs.tsbuildinfo +0 -1
- package/dist/tsconfig.esm.tsbuildinfo +0 -1
- package/dist/tsconfig.types.tsbuildinfo +0 -1
- package/dist/types/FunctionCollection.d.ts +0 -13
- package/dist/types/FunctionCollection.d.ts.map +0 -1
- package/dist/types/base/BackendApi.d.ts +0 -27
- package/dist/types/base/BackendApi.d.ts.map +0 -1
- package/dist/types/base/BackendFunction.d.ts.map +0 -1
- package/dist/types/base/BackendHttp.d.ts.map +0 -1
- package/dist/types/common/firebase.backend.d.ts +0 -3
- package/dist/types/common/firebase.backend.d.ts.map +0 -1
- package/dist/types/demo/User.d.ts +0 -25
- package/dist/types/demo/User.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/lib.d.ts +0 -10
- package/dist/types/lib.d.ts.map +0 -1
- package/dist/types/loader/zod.loader.d.ts +0 -2
- package/dist/types/loader/zod.loader.d.ts.map +0 -1
- package/dist/types/package.d.ts +0 -2
- package/dist/types/package.d.ts.map +0 -1
- package/dist/types/store/Store.d.ts +0 -24
- package/dist/types/store/Store.d.ts.map +0 -1
- package/dist/types/validation/Validation.d.ts +0 -16
- package/dist/types/validation/Validation.d.ts.map +0 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { MetaItem } from "@mxpicture/zod-meta";
|
|
2
|
-
import type { ApiKey, ApiFilter, DocumentData, WithKey } from "@mxpicture/gcp-functions-common";
|
|
3
|
-
import { IApi } from "@mxpicture/gcp-functions-common";
|
|
4
|
-
import { Store } from "../store/Store";
|
|
5
|
-
import type { Validation } from "../validation/Validation";
|
|
2
|
+
import type { ApiKey, ApiFilter, DocumentData, WithKey } from "@mxpicture/gcp-functions-common/types";
|
|
3
|
+
import { IApi } from "@mxpicture/gcp-functions-common/api";
|
|
4
|
+
import { Store } from "../store/Store.js";
|
|
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
8
|
readonly store: STORE;
|
|
@@ -24,4 +24,3 @@ export declare class BackendApi<DTO extends DocumentData, STORE extends Store<DT
|
|
|
24
24
|
id: string;
|
|
25
25
|
}, data: Partial<DTO>): Promise<WithKey<DTO>>;
|
|
26
26
|
}
|
|
27
|
-
//# sourceMappingURL=BackendApi.d.ts.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { Meta } from "@mxpicture/zod-meta";
|
|
2
|
+
import { IApi } from "@mxpicture/gcp-functions-common/api";
|
|
3
|
+
export class BackendApi extends IApi {
|
|
4
|
+
store;
|
|
5
|
+
validation;
|
|
6
|
+
shape;
|
|
7
7
|
constructor(store, validation, shape) {
|
|
8
8
|
super();
|
|
9
9
|
this.store = store;
|
|
@@ -26,7 +26,7 @@ class BackendApi extends gcp_functions_common_1.IApi {
|
|
|
26
26
|
return { exists: await this.store.exists(p.id) };
|
|
27
27
|
}
|
|
28
28
|
async meta() {
|
|
29
|
-
return new
|
|
29
|
+
return new Meta(this.shape).items();
|
|
30
30
|
}
|
|
31
31
|
async create(data) {
|
|
32
32
|
const result = await this.validation.validate(data);
|
|
@@ -37,4 +37,3 @@ class BackendApi extends gcp_functions_common_1.IApi {
|
|
|
37
37
|
return this.store.update(p.id, result);
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
|
-
exports.BackendApi = BackendApi;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BackendApi } from "./BackendApi.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BackendApi } from "./BackendApi.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { app, db } from "./firebase.backend.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { app, db } from "./firebase.backend.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { HttpsFunction } from "firebase-functions/https";
|
|
2
|
-
import type { HttpHandlers, DocumentData, ApiRoutes } from "@mxpicture/gcp-functions-common";
|
|
2
|
+
import type { HttpHandlers, DocumentData, ApiRoutes } 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
5
|
protected http: HANDLER;
|
|
@@ -7,4 +7,3 @@ export declare class BackendFunction<DTO extends DocumentData, HANDLER extends H
|
|
|
7
7
|
constructor(name: string, http: HANDLER);
|
|
8
8
|
createFunction(): HttpsFunction;
|
|
9
9
|
}
|
|
10
|
-
//# sourceMappingURL=BackendFunction.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { HttpsError, onCall } from "firebase-functions/https";
|
|
2
|
+
import { API_ROUTES_METHODS } from "@mxpicture/gcp-functions-common/config";
|
|
3
|
+
export class BackendFunction {
|
|
4
|
+
name;
|
|
5
|
+
http;
|
|
6
|
+
httpMethods;
|
|
7
|
+
constructor(name, http) {
|
|
8
|
+
this.name = name;
|
|
9
|
+
this.http = http;
|
|
10
|
+
this.httpMethods = API_ROUTES_METHODS;
|
|
11
|
+
}
|
|
12
|
+
createFunction() {
|
|
13
|
+
return onCall({ cors: [/firebase\.com$/, /web\.app$/] }, async (req) => {
|
|
14
|
+
if (!this.httpMethods.find((m) => m === req.data.route))
|
|
15
|
+
throw new HttpsError("invalid-argument", `Route ${req.data.route} not available`);
|
|
16
|
+
return {
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
doc: await this.http[req.data.route](req.data),
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BackendFunction } from "./
|
|
2
|
-
import type { DocumentData } from "@mxpicture/gcp-functions-common";
|
|
1
|
+
import { BackendFunction } from "./BackendFunction.js";
|
|
2
|
+
import type { DocumentData } from "@mxpicture/gcp-functions-common/types";
|
|
3
3
|
export declare class FunctionCollection {
|
|
4
4
|
protected funcs: {
|
|
5
5
|
[key: string]: BackendFunction<any>;
|
|
@@ -7,7 +7,6 @@ export declare class FunctionCollection {
|
|
|
7
7
|
constructor();
|
|
8
8
|
add<DTO extends DocumentData>(func: BackendFunction<DTO>, name?: string): FunctionCollection;
|
|
9
9
|
build(): {
|
|
10
|
-
[key: string]: BackendFunction<any, import("@mxpicture/gcp-functions-common").HttpHandlers<import("@mxpicture/gcp-functions-common").ApiRoutes<any>>>;
|
|
10
|
+
[key: string]: BackendFunction<any, import("@mxpicture/gcp-functions-common/types").HttpHandlers<import("@mxpicture/gcp-functions-common/types").ApiRoutes<any>>>;
|
|
11
11
|
};
|
|
12
12
|
}
|
|
13
|
-
//# sourceMappingURL=FunctionCollection.d.ts.map
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { HttpHandlers, FunctionPayload, ApiFilter, ApiKey, ApiRoutes, DocumentData } from "@mxpicture/gcp-functions-common";
|
|
2
|
-
import { BackendApi } from "
|
|
1
|
+
import type { HttpHandlers, FunctionPayload, ApiFilter, ApiKey, ApiRoutes, DocumentData } from "@mxpicture/gcp-functions-common/types";
|
|
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
4
|
protected api: API;
|
|
5
5
|
constructor(api: API);
|
|
6
6
|
delete(req: FunctionPayload<never, ApiKey>): Promise<void>;
|
|
7
|
-
get(req: FunctionPayload<never, ApiKey>): Promise<import("@mxpicture/gcp-functions-common").WithKey<DTO>>;
|
|
8
|
-
query(req: FunctionPayload<ApiFilter>): Promise<import("@mxpicture/gcp-functions-common").WithKey<DTO>[]>;
|
|
7
|
+
get(req: FunctionPayload<never, ApiKey>): Promise<import("@mxpicture/gcp-functions-common/types").WithKey<DTO>>;
|
|
8
|
+
query(req: FunctionPayload<ApiFilter>): Promise<import("@mxpicture/gcp-functions-common/types").WithKey<DTO>[]>;
|
|
9
9
|
count(req: FunctionPayload<ApiFilter>): Promise<{
|
|
10
10
|
count: number;
|
|
11
11
|
}>;
|
|
@@ -13,7 +13,6 @@ export declare class BackendHttp<DTO extends DocumentData, API extends BackendAp
|
|
|
13
13
|
exists: boolean;
|
|
14
14
|
}>;
|
|
15
15
|
meta(): Promise<import("@mxpicture/zod-meta").MetaItem[]>;
|
|
16
|
-
create(req: FunctionPayload<DTO>): Promise<import("@mxpicture/gcp-functions-common").WithKey<DTO>>;
|
|
17
|
-
update(req: FunctionPayload<Partial<DTO>, ApiKey>): Promise<import("@mxpicture/gcp-functions-common").WithKey<DTO>>;
|
|
16
|
+
create(req: FunctionPayload<DTO>): Promise<import("@mxpicture/gcp-functions-common/types").WithKey<DTO>>;
|
|
17
|
+
update(req: FunctionPayload<Partial<DTO>, ApiKey>): Promise<import("@mxpicture/gcp-functions-common/types").WithKey<DTO>>;
|
|
18
18
|
}
|
|
19
|
-
//# sourceMappingURL=BackendHttp.d.ts.map
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { HttpsError } from "firebase-functions/https";
|
|
2
|
+
export class BackendHttp {
|
|
3
|
+
api;
|
|
4
|
+
constructor(api) {
|
|
5
|
+
this.api = api;
|
|
6
|
+
}
|
|
7
|
+
async delete(req) {
|
|
8
|
+
try {
|
|
9
|
+
const id = req.keys?.id;
|
|
10
|
+
if (!id)
|
|
11
|
+
throw new HttpsError("invalid-argument", "Id invalid");
|
|
12
|
+
await this.api.delete({ id });
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error instanceof HttpsError)
|
|
16
|
+
throw error;
|
|
17
|
+
throw new HttpsError("not-found", `Id ${req.keys?.id} not found`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async get(req) {
|
|
21
|
+
try {
|
|
22
|
+
const id = req.keys?.id;
|
|
23
|
+
if (!id)
|
|
24
|
+
throw new HttpsError("invalid-argument", "Id invalid");
|
|
25
|
+
return this.api.get({ id });
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (error instanceof HttpsError)
|
|
29
|
+
throw error;
|
|
30
|
+
throw new HttpsError("not-found", `Id ${req.keys?.id} not found`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async query(req) {
|
|
34
|
+
try {
|
|
35
|
+
return this.api.query(req.content);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (error instanceof HttpsError)
|
|
39
|
+
throw error;
|
|
40
|
+
throw new HttpsError("unknown", "Unknown error");
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async count(req) {
|
|
44
|
+
try {
|
|
45
|
+
return this.api.count(req.content);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error instanceof HttpsError)
|
|
49
|
+
throw error;
|
|
50
|
+
throw new HttpsError("unknown", "Unknown error");
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async exists(req) {
|
|
54
|
+
try {
|
|
55
|
+
const id = req.keys?.id;
|
|
56
|
+
if (!id)
|
|
57
|
+
throw new HttpsError("invalid-argument", "Id invalid");
|
|
58
|
+
return await this.api.exists({ id });
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
if (error instanceof HttpsError)
|
|
62
|
+
throw error;
|
|
63
|
+
throw new HttpsError("unknown", "Unknown error");
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async meta() {
|
|
67
|
+
try {
|
|
68
|
+
return this.api.meta();
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof HttpsError)
|
|
72
|
+
throw error;
|
|
73
|
+
throw new HttpsError("unknown", "Unknown error");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async create(req) {
|
|
77
|
+
try {
|
|
78
|
+
if (!req.content)
|
|
79
|
+
throw new HttpsError("invalid-argument", "Content is invalid");
|
|
80
|
+
return this.api.create(req.content);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error instanceof HttpsError)
|
|
84
|
+
throw error;
|
|
85
|
+
throw new HttpsError("unknown", "Unknown error");
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async update(req) {
|
|
89
|
+
try {
|
|
90
|
+
const id = req.keys?.id;
|
|
91
|
+
if (!id)
|
|
92
|
+
throw new HttpsError("invalid-argument", "Id invalid");
|
|
93
|
+
if (!req.content)
|
|
94
|
+
throw new HttpsError("invalid-argument", "Content is invalid");
|
|
95
|
+
return this.api.update({ id }, req.content);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (error instanceof HttpsError)
|
|
99
|
+
throw error;
|
|
100
|
+
throw new HttpsError("unknown", "Unknown error");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BackendHttp } from "./BackendHttp.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BackendHttp } from "./BackendHttp.js";
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CollectionReference, DocumentReference, FirestoreDataConverter, Query, QuerySnapshot } from "firebase-admin/firestore";
|
|
2
|
-
import type { DocumentData, WithKey, ApiFilterItems } from "@mxpicture/gcp-functions-common";
|
|
2
|
+
import type { DocumentData, WithKey, ApiFilterItems } from "@mxpicture/gcp-functions-common/types";
|
|
3
3
|
export declare class Store<DTO extends DocumentData> {
|
|
4
4
|
readonly collectionName: string;
|
|
5
5
|
protected readonly converter?: FirestoreDataConverter<DTO> | undefined;
|
|
@@ -21,4 +21,3 @@ export declare class Store<DTO extends DocumentData> {
|
|
|
21
21
|
deleteByParent(parentId: string): Promise<void>;
|
|
22
22
|
refByParent(parentId: string): Query<DTO, FirebaseFirestore.DocumentData>;
|
|
23
23
|
}
|
|
24
|
-
//# sourceMappingURL=Store.d.ts.map
|
|
@@ -1,42 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.Store = void 0;
|
|
37
|
-
const firebase_backend_js_1 = require("../common/firebase.backend.js");
|
|
38
|
-
const uuid = __importStar(require("short-uuid"));
|
|
39
|
-
class Store {
|
|
1
|
+
import { db as firestore } from "../firebase/firebase.backend.js";
|
|
2
|
+
import * as uuid from "short-uuid";
|
|
3
|
+
export class Store {
|
|
4
|
+
collectionName;
|
|
5
|
+
converter;
|
|
6
|
+
namespace;
|
|
7
|
+
_db;
|
|
40
8
|
constructor(collectionName, converter, namespace = "picpad") {
|
|
41
9
|
this.collectionName = collectionName;
|
|
42
10
|
this.converter = converter;
|
|
@@ -45,11 +13,11 @@ class Store {
|
|
|
45
13
|
db() {
|
|
46
14
|
if (!this._db)
|
|
47
15
|
if (this.converter)
|
|
48
|
-
this._db =
|
|
16
|
+
this._db = firestore
|
|
49
17
|
.collection(`${this.namespace}.${this.collectionName}`)
|
|
50
18
|
.withConverter(this.converter);
|
|
51
19
|
else
|
|
52
|
-
this._db =
|
|
20
|
+
this._db = firestore.collection(`${this.namespace}.${this.collectionName}`);
|
|
53
21
|
return this._db;
|
|
54
22
|
}
|
|
55
23
|
async query(p) {
|
|
@@ -125,4 +93,3 @@ class Store {
|
|
|
125
93
|
return this.db().where("parentId", "==", parentId);
|
|
126
94
|
}
|
|
127
95
|
}
|
|
128
|
-
exports.Store = Store;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Store } from "./Store.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Store } from "./Store.js";
|
package/dist/types/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ValidationFieldErrors = Record<string, string[]>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Store } from "../store/Store";
|
|
1
|
+
import { Store } from "../store/Store.js";
|
|
2
2
|
import type { ZodError, ZodObject, ZodRawShape } from "zod";
|
|
3
|
-
import type { DocumentData, WithKey } from "@mxpicture/gcp-functions-common";
|
|
4
|
-
|
|
3
|
+
import type { DocumentData, WithKey } from "@mxpicture/gcp-functions-common/types";
|
|
4
|
+
import { ValidationFieldErrors } from "../types/types.validation.js";
|
|
5
5
|
export declare const createValidation: <DTO extends DocumentData>(store: Store<DTO>, shape: ZodRawShape) => Promise<Validation<DTO>>;
|
|
6
6
|
export declare class Validation<DTO extends DocumentData> {
|
|
7
7
|
readonly store: Store<DTO>;
|
|
@@ -13,4 +13,3 @@ export declare class Validation<DTO extends DocumentData> {
|
|
|
13
13
|
validatePartial(doc: Partial<WithKey<DTO>>): Promise<Partial<WithKey<DTO>>>;
|
|
14
14
|
protected issuesToFieldErrors(issues: ZodError["issues"]): ValidationFieldErrors;
|
|
15
15
|
}
|
|
16
|
-
//# sourceMappingURL=Validation.d.ts.map
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const zod_loader_1 = require("../loader/zod.loader");
|
|
6
|
-
const createValidation = async (store, shape) => {
|
|
7
|
-
const createSchema = (await (0, zod_loader_1.loadZod)()).object(shape);
|
|
1
|
+
import { HttpsError } from "firebase-functions/https";
|
|
2
|
+
import { loadZod } from "../loader/zod.loader.js";
|
|
3
|
+
export const createValidation = async (store, shape) => {
|
|
4
|
+
const createSchema = (await loadZod()).object(shape);
|
|
8
5
|
return new Validation(store, shape, createSchema, createSchema.partial());
|
|
9
6
|
};
|
|
10
|
-
|
|
11
|
-
|
|
7
|
+
export class Validation {
|
|
8
|
+
store;
|
|
9
|
+
shape;
|
|
10
|
+
createSchema;
|
|
11
|
+
updateSchema;
|
|
12
12
|
constructor(store, shape, createSchema, updateSchema) {
|
|
13
13
|
this.store = store;
|
|
14
14
|
this.shape = shape;
|
|
@@ -18,7 +18,7 @@ class Validation {
|
|
|
18
18
|
async validate(doc) {
|
|
19
19
|
const result = this.createSchema.safeParse(doc);
|
|
20
20
|
if (!result.success)
|
|
21
|
-
throw new
|
|
21
|
+
throw new HttpsError("invalid-argument", "Invalid request data", {
|
|
22
22
|
fields: this.issuesToFieldErrors(result.error.issues),
|
|
23
23
|
});
|
|
24
24
|
return result.data;
|
|
@@ -26,7 +26,7 @@ class Validation {
|
|
|
26
26
|
async validatePartial(doc) {
|
|
27
27
|
const result = this.updateSchema.safeParse(doc);
|
|
28
28
|
if (!result.success)
|
|
29
|
-
throw new
|
|
29
|
+
throw new HttpsError("invalid-argument", "Invalid request data", {
|
|
30
30
|
fields: this.issuesToFieldErrors(result.error.issues),
|
|
31
31
|
});
|
|
32
32
|
return result.data;
|
|
@@ -35,9 +35,8 @@ class Validation {
|
|
|
35
35
|
const fields = {};
|
|
36
36
|
for (const issue of issues) {
|
|
37
37
|
const path = issue.path.length ? issue.path.join(".") : "_form";
|
|
38
|
-
(fields[path]
|
|
38
|
+
(fields[path] ??= []).push(issue.message);
|
|
39
39
|
}
|
|
40
40
|
return fields;
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
-
exports.Validation = Validation;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Validation, createValidation } from "./Validation.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Validation, createValidation } from "./Validation.js";
|
package/package.json
CHANGED
|
@@ -1,40 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mxpicture/gcp-functions-backend",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"description": "Utils for google cloud functions, publishing both CommonJS and ESM builds",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"author": "MXPicture",
|
|
6
7
|
"license": "MIT",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
9
10
|
"url": "git+https://github.com/MXPicture/npm-gcp-functions.git"
|
|
10
11
|
},
|
|
11
|
-
"main": "dist/cjs/index.js",
|
|
12
|
-
"module": "dist/esm/index.js",
|
|
13
|
-
"types": "dist/types/package.d.ts",
|
|
14
12
|
"exports": {
|
|
15
|
-
"
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
"import": "./dist/esm/*",
|
|
23
|
-
"require": "./dist/cjs/*"
|
|
24
|
-
},
|
|
13
|
+
"./store": "./dist/store/index.js",
|
|
14
|
+
"./firebase": "./dist/firebase/index.js",
|
|
15
|
+
"./validation": "./dist/validation/index.js",
|
|
16
|
+
"./api": "./dist/api/index.js",
|
|
17
|
+
"./http": "./dist/http/index.js",
|
|
18
|
+
"./function": "./dist/function/index.js",
|
|
19
|
+
"./types": "./dist/types/index.js",
|
|
25
20
|
"./package.json": "./package.json"
|
|
26
21
|
},
|
|
27
22
|
"files": [
|
|
28
|
-
"dist
|
|
23
|
+
"dist"
|
|
29
24
|
],
|
|
30
25
|
"engines": {
|
|
31
26
|
"node": "24"
|
|
32
27
|
},
|
|
33
28
|
"scripts": {
|
|
34
|
-
"clean": "rm -rf dist .tsbuildinfo",
|
|
29
|
+
"clean": "rm -rf dist .tsbuildinfo node_modules",
|
|
35
30
|
"lint": "eslint \"src/**/*.{ts,tsx}\" --ext .ts,.tsx",
|
|
36
31
|
"update": "ncu -u && npm install",
|
|
37
|
-
"build": "tsc",
|
|
32
|
+
"build": "tsc -p tsconfig.json",
|
|
38
33
|
"update-own": "npm update --save @mxpicture/zod-meta"
|
|
39
34
|
},
|
|
40
35
|
"publishConfig": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"FunctionCollection.d.ts","sourceRoot":"","sources":["../../src/FunctionCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iCAAiC,CAAC;AAEpE,qBAAa,kBAAkB;IAE7B,SAAS,CAAC,KAAK,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,CAAC,GAAG,CAAC,CAAA;KAAE,CAAM;;IAIvD,GAAG,CAAC,GAAG,SAAS,YAAY,EACjC,IAAI,EAAE,eAAe,CAAC,GAAG,CAAC,EAC1B,IAAI,CAAC,EAAE,MAAM,GACZ,kBAAkB;IAKd,KAAK;;;CAGb"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FunctionCollection = void 0;
|
|
4
|
-
class FunctionCollection {
|
|
5
|
-
constructor() {
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
-
this.funcs = {};
|
|
8
|
-
}
|
|
9
|
-
add(func, name) {
|
|
10
|
-
this.funcs[name ?? func.name] = func;
|
|
11
|
-
return this;
|
|
12
|
-
}
|
|
13
|
-
build() {
|
|
14
|
-
return this.funcs;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
exports.FunctionCollection = FunctionCollection;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { MetaItem } from "@mxpicture/zod-meta";
|
|
2
|
-
import type { ApiKey, ApiFilter, DocumentData, WithKey } from "@mxpicture/gcp-functions-common";
|
|
3
|
-
import { IApi } from "@mxpicture/gcp-functions-common";
|
|
4
|
-
import { Store } from "../store/Store";
|
|
5
|
-
import type { Validation } from "../validation/Validation";
|
|
6
|
-
import type { ZodRawShape } from "zod";
|
|
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);
|
|
12
|
-
delete(p: ApiKey): Promise<void>;
|
|
13
|
-
get(p: ApiKey): Promise<WithKey<DTO>>;
|
|
14
|
-
query(payload?: ApiFilter): Promise<WithKey<DTO>[]>;
|
|
15
|
-
count(payload?: ApiFilter): Promise<{
|
|
16
|
-
count: number;
|
|
17
|
-
}>;
|
|
18
|
-
exists(p: ApiKey): Promise<{
|
|
19
|
-
exists: boolean;
|
|
20
|
-
}>;
|
|
21
|
-
meta(): Promise<MetaItem[]>;
|
|
22
|
-
create(data: DTO): Promise<WithKey<DTO>>;
|
|
23
|
-
update(p: {
|
|
24
|
-
id: string;
|
|
25
|
-
}, data: Partial<DTO>): Promise<WithKey<DTO>>;
|
|
26
|
-
}
|
|
27
|
-
//# sourceMappingURL=BackendApi.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BackendApi.d.ts","sourceRoot":"","sources":["../../../src/base/BackendApi.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,KAAK,EACV,MAAM,EACN,SAAS,EACT,YAAY,EACZ,OAAO,EACR,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,iCAAiC,CAAC;AACvD,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAEvC,qBAAa,UAAU,CACrB,GAAG,SAAS,YAAY,EACxB,KAAK,SAAS,KAAK,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,EACrC,GAAG,SAAS,UAAU,CAAC,GAAG,CAAC,GAAG,UAAU,CAAC,GAAG,CAAC,CAC7C,SAAQ,IAAI,CAAC,GAAG,CAAC;aAEC,KAAK,EAAE,KAAK;aACZ,UAAU,EAAE,GAAG;IAC/B,SAAS,CAAC,KAAK,EAAE,WAAW;gBAFZ,KAAK,EAAE,KAAK,EACZ,UAAU,EAAE,GAAG,EACrB,KAAK,EAAE,WAAW;IAKR,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIhC,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAIrC,KAAK,CAAC,OAAO,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;IAInD,KAAK,CAAC,OAAO,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAItD,MAAM,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,OAAO,CAAA;KAAE,CAAC;IAI/C,IAAI,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;IAI3B,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAKxC,MAAM,CAAC,CAAC,EAAE;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,EAAE,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC;CAInE"}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.BackendApi = void 0;
|
|
4
|
-
const zod_meta_1 = require("@mxpicture/zod-meta");
|
|
5
|
-
const gcp_functions_common_1 = require("@mxpicture/gcp-functions-common");
|
|
6
|
-
class BackendApi extends gcp_functions_common_1.IApi {
|
|
7
|
-
constructor(store, validation, shape) {
|
|
8
|
-
super();
|
|
9
|
-
this.store = store;
|
|
10
|
-
this.validation = validation;
|
|
11
|
-
this.shape = shape;
|
|
12
|
-
}
|
|
13
|
-
async delete(p) {
|
|
14
|
-
return this.store.delete(p.id);
|
|
15
|
-
}
|
|
16
|
-
async get(p) {
|
|
17
|
-
return this.store.get(p.id);
|
|
18
|
-
}
|
|
19
|
-
async query(payload) {
|
|
20
|
-
return this.store.query(payload?.filters);
|
|
21
|
-
}
|
|
22
|
-
async count(payload) {
|
|
23
|
-
return { count: await this.store.count(payload?.filters) };
|
|
24
|
-
}
|
|
25
|
-
async exists(p) {
|
|
26
|
-
return { exists: await this.store.exists(p.id) };
|
|
27
|
-
}
|
|
28
|
-
async meta() {
|
|
29
|
-
return new zod_meta_1.Meta(this.shape).items();
|
|
30
|
-
}
|
|
31
|
-
async create(data) {
|
|
32
|
-
const result = await this.validation.validate(data);
|
|
33
|
-
return this.store.create(result);
|
|
34
|
-
}
|
|
35
|
-
async update(p, data) {
|
|
36
|
-
const result = await this.validation.validatePartial(data);
|
|
37
|
-
return this.store.update(p.id, result);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.BackendApi = BackendApi;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { HttpsFunction } from "firebase-functions/https";
|
|
2
|
-
import type { HttpHandlers, DocumentData, ApiRoutes } from "@mxpicture/gcp-functions-common";
|
|
3
|
-
export declare class BackendFunction<DTO extends DocumentData, HANDLER extends HttpHandlers<ApiRoutes<DTO>> = HttpHandlers<ApiRoutes<DTO>>> {
|
|
4
|
-
readonly name: string;
|
|
5
|
-
protected http: HANDLER;
|
|
6
|
-
protected readonly httpMethods: string[];
|
|
7
|
-
constructor(name: string, http: HANDLER);
|
|
8
|
-
createFunction(): HttpsFunction;
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=BackendFunction.d.ts.map
|