firebase-rest-firestore 0.1.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.
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertToFirestoreValue = convertToFirestoreValue;
4
+ exports.convertFromFirestoreValue = convertFromFirestoreValue;
5
+ exports.convertToFirestoreDocument = convertToFirestoreDocument;
6
+ exports.convertFromFirestoreDocument = convertFromFirestoreDocument;
7
+ const path_1 = require("./path");
8
+ /**
9
+ * JSの値をFirestore形式に変換する
10
+ * @param value 変換する値
11
+ * @returns Firestore形式の値
12
+ */
13
+ function convertToFirestoreValue(value) {
14
+ if (value instanceof Date) {
15
+ return { timestampValue: value.toISOString() };
16
+ }
17
+ else if (typeof value === "string") {
18
+ return { stringValue: value };
19
+ }
20
+ else if (typeof value === "number") {
21
+ return Number.isInteger(value)
22
+ ? { integerValue: value }
23
+ : { doubleValue: value };
24
+ }
25
+ else if (typeof value === "boolean") {
26
+ return { booleanValue: value };
27
+ }
28
+ else if (value === null || value === undefined) {
29
+ return { nullValue: null };
30
+ }
31
+ else if (Array.isArray(value)) {
32
+ return {
33
+ arrayValue: {
34
+ values: value.map(item => convertToFirestoreValue(item)),
35
+ },
36
+ };
37
+ }
38
+ else if (typeof value === "object") {
39
+ const fields = Object.entries(value).reduce((acc, [key, val]) => ({
40
+ ...acc,
41
+ [key]: convertToFirestoreValue(val),
42
+ }), {});
43
+ return { mapValue: { fields } };
44
+ }
45
+ // デフォルトは文字列化
46
+ return { stringValue: String(value) };
47
+ }
48
+ /**
49
+ * Firestore形式からJSの値に変換する
50
+ * @param firestoreValue Firestore形式の値
51
+ * @returns JS形式の値
52
+ */
53
+ function convertFromFirestoreValue(firestoreValue) {
54
+ if ("stringValue" in firestoreValue) {
55
+ return firestoreValue.stringValue;
56
+ }
57
+ else if ("integerValue" in firestoreValue) {
58
+ return Number(firestoreValue.integerValue);
59
+ }
60
+ else if ("doubleValue" in firestoreValue) {
61
+ return firestoreValue.doubleValue;
62
+ }
63
+ else if ("booleanValue" in firestoreValue) {
64
+ return firestoreValue.booleanValue;
65
+ }
66
+ else if ("nullValue" in firestoreValue) {
67
+ return null;
68
+ }
69
+ else if ("timestampValue" in firestoreValue) {
70
+ return new Date(firestoreValue.timestampValue);
71
+ }
72
+ else if ("mapValue" in firestoreValue && firestoreValue.mapValue.fields) {
73
+ return Object.entries(firestoreValue.mapValue.fields).reduce((acc, [key, val]) => ({
74
+ ...acc,
75
+ [key]: convertFromFirestoreValue(val),
76
+ }), {});
77
+ }
78
+ else if ("arrayValue" in firestoreValue &&
79
+ firestoreValue.arrayValue.values) {
80
+ return firestoreValue.arrayValue.values.map(convertFromFirestoreValue);
81
+ }
82
+ return null;
83
+ }
84
+ /**
85
+ * オブジェクトをFirestoreドキュメント形式に変換
86
+ * @param data 変換するオブジェクト
87
+ * @returns Firestoreドキュメント
88
+ */
89
+ function convertToFirestoreDocument(data) {
90
+ return {
91
+ fields: Object.entries(data).reduce((acc, [key, value]) => ({
92
+ ...acc,
93
+ [key]: convertToFirestoreValue(value),
94
+ }), {}),
95
+ };
96
+ }
97
+ /**
98
+ * Firestoreドキュメントをオブジェクトに変換
99
+ * @param doc Firestoreレスポンス
100
+ * @returns 変換されたオブジェクト(idプロパティ付き)
101
+ */
102
+ function convertFromFirestoreDocument(doc) {
103
+ if (!doc.fields)
104
+ return { id: (0, path_1.getDocumentId)(doc.name) };
105
+ const result = Object.entries(doc.fields).reduce((acc, [key, value]) => ({
106
+ ...acc,
107
+ [key]: convertFromFirestoreValue(value),
108
+ }), {});
109
+ return {
110
+ ...result,
111
+ id: (0, path_1.getDocumentId)(doc.name),
112
+ };
113
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Firestoreのベースパスを取得
3
+ * @param projectId プロジェクトID
4
+ * @param collectionName コレクション名(省略可)
5
+ * @returns Firestoreのベースパス
6
+ */
7
+ export declare function getFirestoreBasePath(projectId: string, collectionName?: string): string;
8
+ /**
9
+ * ドキュメントパスからIDを抽出
10
+ * @param path ドキュメントパス
11
+ * @returns ドキュメントID
12
+ */
13
+ export declare function getDocumentId(path?: string): string;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getFirestoreBasePath = getFirestoreBasePath;
4
+ exports.getDocumentId = getDocumentId;
5
+ /**
6
+ * Firestoreのベースパスを取得
7
+ * @param projectId プロジェクトID
8
+ * @param collectionName コレクション名(省略可)
9
+ * @returns Firestoreのベースパス
10
+ */
11
+ function getFirestoreBasePath(projectId, collectionName) {
12
+ const basePath = `https://firestore.googleapis.com/v1/projects/${projectId}/databases/(default)/documents`;
13
+ return collectionName ? `${basePath}/${collectionName}` : basePath;
14
+ }
15
+ /**
16
+ * ドキュメントパスからIDを抽出
17
+ * @param path ドキュメントパス
18
+ * @returns ドキュメントID
19
+ */
20
+ function getDocumentId(path) {
21
+ if (!path)
22
+ return "";
23
+ const pathParts = path.split("/");
24
+ return pathParts[pathParts.length - 1];
25
+ }
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "firebase-rest-firestore",
3
+ "version": "0.1.0",
4
+ "description": "Firebase Firestore REST API client for Edge runtime environments",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist",
9
+ "README.md"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "prepublishOnly": "npm run build",
14
+ "test": "echo \"Error: no test specified\" && exit 1"
15
+ },
16
+ "keywords": [
17
+ "firebase",
18
+ "firestore",
19
+ "rest",
20
+ "api",
21
+ "edge",
22
+ "cloudflare",
23
+ "workers",
24
+ "vercel"
25
+ ],
26
+ "author": "",
27
+ "license": "MIT",
28
+ "dependencies": {
29
+ "jose": "^4.14.4"
30
+ },
31
+ "devDependencies": {
32
+ "@types/node": "^18.16.0",
33
+ "typescript": "^5.0.4"
34
+ },
35
+ "engines": {
36
+ "node": ">=16.0.0"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/yourusername/firebase-rest-firestore.git"
41
+ },
42
+ "bugs": {
43
+ "url": "https://github.com/yourusername/firebase-rest-firestore/issues"
44
+ },
45
+ "homepage": "https://github.com/yourusername/firebase-rest-firestore#readme"
46
+ }