firebase-rest-firestore 1.2.0 → 1.5.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.
Files changed (41) hide show
  1. package/dist/cjs/client.d.ts +417 -0
  2. package/dist/{client.js → cjs/client.js} +442 -279
  3. package/dist/{index.d.ts → cjs/index.d.ts} +1 -1
  4. package/dist/{index.js → cjs/index.js} +1 -2
  5. package/dist/{types.d.ts → cjs/types.d.ts} +4 -0
  6. package/dist/cjs/utils/auth.d.ts +13 -0
  7. package/dist/{utils → cjs/utils}/auth.js +14 -10
  8. package/dist/cjs/utils/config.d.ts +6 -0
  9. package/dist/{utils → cjs/utils}/config.js +0 -12
  10. package/dist/cjs/utils/path.d.ts +73 -0
  11. package/dist/cjs/utils/path.js +176 -0
  12. package/dist/esm/client.d.ts +417 -0
  13. package/dist/esm/client.js +1066 -0
  14. package/dist/esm/index.d.ts +7 -0
  15. package/dist/esm/index.js +11 -0
  16. package/dist/esm/types.d.ts +69 -0
  17. package/dist/esm/types.js +1 -0
  18. package/dist/esm/utils/auth.d.ts +13 -0
  19. package/dist/esm/utils/auth.js +57 -0
  20. package/dist/esm/utils/config.d.ts +6 -0
  21. package/dist/esm/utils/config.js +11 -0
  22. package/dist/esm/utils/converter.d.ts +27 -0
  23. package/dist/esm/utils/converter.js +107 -0
  24. package/dist/esm/utils/path.d.ts +73 -0
  25. package/dist/esm/utils/path.js +169 -0
  26. package/dist/types/client.d.ts +417 -0
  27. package/dist/types/index.d.ts +7 -0
  28. package/dist/types/types.d.ts +69 -0
  29. package/dist/types/utils/auth.d.ts +13 -0
  30. package/dist/types/utils/config.d.ts +6 -0
  31. package/dist/types/utils/converter.d.ts +27 -0
  32. package/dist/types/utils/path.d.ts +73 -0
  33. package/package.json +18 -5
  34. package/dist/client.d.ts +0 -381
  35. package/dist/utils/auth.d.ts +0 -13
  36. package/dist/utils/config.d.ts +0 -13
  37. package/dist/utils/path.d.ts +0 -14
  38. package/dist/utils/path.js +0 -27
  39. /package/dist/{types.js → cjs/types.js} +0 -0
  40. /package/dist/{utils → cjs/utils}/converter.d.ts +0 -0
  41. /package/dist/{utils → cjs/utils}/converter.js +0 -0
@@ -3,5 +3,5 @@ import { FirestoreClient, createFirestoreClient, CollectionReference, DocumentRe
3
3
  export { getFirestoreToken } from "./utils/auth";
4
4
  export { convertToFirestoreValue, convertFromFirestoreValue, convertToFirestoreDocument, convertFromFirestoreDocument, } from "./utils/converter";
5
5
  export { getFirestoreBasePath, getDocumentId } from "./utils/path";
6
- export { formatPrivateKey, formatConfig } from "./utils/config";
6
+ export { formatPrivateKey } from "./utils/config";
7
7
  export { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult, };
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.WriteResult = exports.DocumentSnapshot = exports.QuerySnapshot = exports.Query = exports.CollectionGroup = exports.DocumentReference = exports.CollectionReference = exports.createFirestoreClient = exports.FirestoreClient = exports.formatConfig = exports.formatPrivateKey = exports.getDocumentId = exports.getFirestoreBasePath = exports.convertFromFirestoreDocument = exports.convertToFirestoreDocument = exports.convertFromFirestoreValue = exports.convertToFirestoreValue = exports.getFirestoreToken = void 0;
17
+ exports.WriteResult = exports.DocumentSnapshot = exports.QuerySnapshot = exports.Query = exports.CollectionGroup = exports.DocumentReference = exports.CollectionReference = exports.createFirestoreClient = exports.FirestoreClient = exports.formatPrivateKey = exports.getDocumentId = exports.getFirestoreBasePath = exports.convertFromFirestoreDocument = exports.convertToFirestoreDocument = exports.convertFromFirestoreValue = exports.convertToFirestoreValue = exports.getFirestoreToken = void 0;
18
18
  // 型定義のエクスポート
19
19
  __exportStar(require("./types"), exports);
20
20
  // クライアントのエクスポート
@@ -41,4 +41,3 @@ Object.defineProperty(exports, "getFirestoreBasePath", { enumerable: true, get:
41
41
  Object.defineProperty(exports, "getDocumentId", { enumerable: true, get: function () { return path_1.getDocumentId; } });
42
42
  var config_1 = require("./utils/config");
43
43
  Object.defineProperty(exports, "formatPrivateKey", { enumerable: true, get: function () { return config_1.formatPrivateKey; } });
44
- Object.defineProperty(exports, "formatConfig", { enumerable: true, get: function () { return config_1.formatConfig; } });
@@ -6,6 +6,10 @@ export interface FirestoreConfig {
6
6
  privateKey: string;
7
7
  clientEmail: string;
8
8
  databaseId?: string;
9
+ debug?: boolean;
10
+ useEmulator?: boolean;
11
+ emulatorHost?: string;
12
+ emulatorPort?: number;
9
13
  }
10
14
  /**
11
15
  * Firestoreの値型定義
@@ -0,0 +1,13 @@
1
+ import { FirestoreConfig } from "../types";
2
+ /**
3
+ * Function to create a JWT (JSON Web Token)
4
+ * @param config Firestore configuration
5
+ * @returns JWT string
6
+ */
7
+ export declare function createJWT(config: FirestoreConfig): Promise<string>;
8
+ /**
9
+ * Function to get Firestore authentication token
10
+ * @param config Firestore configuration
11
+ * @returns Access token
12
+ */
13
+ export declare function getFirestoreToken(config: FirestoreConfig): Promise<string>;
@@ -37,9 +37,9 @@ exports.createJWT = createJWT;
37
37
  exports.getFirestoreToken = getFirestoreToken;
38
38
  const jose = __importStar(require("jose"));
39
39
  /**
40
- * JWT(JSON Web Token)を作成する関数
41
- * @param config Firestore設定
42
- * @returns JWT文字列
40
+ * Function to create a JWT (JSON Web Token)
41
+ * @param config Firestore configuration
42
+ * @returns JWT string
43
43
  */
44
44
  async function createJWT(config) {
45
45
  const now = Math.floor(Date.now() / 1000);
@@ -48,13 +48,13 @@ async function createJWT(config) {
48
48
  sub: config.clientEmail,
49
49
  aud: "https://oauth2.googleapis.com/token",
50
50
  iat: now,
51
- exp: now + 3600, // 1時間後に期限切れ
51
+ exp: now + 3600, // Expires in 1 hour
52
52
  scope: "https://www.googleapis.com/auth/datastore",
53
53
  };
54
54
  try {
55
- // 秘密鍵をインポート
55
+ // Import the private key
56
56
  const privateKey = await jose.importPKCS8(config.privateKey, "RS256");
57
- // JWTを作成
57
+ // Create JWT
58
58
  const token = await new jose.SignJWT(payload)
59
59
  .setProtectedHeader({
60
60
  alg: "RS256",
@@ -69,12 +69,16 @@ async function createJWT(config) {
69
69
  }
70
70
  }
71
71
  /**
72
- * Firestoreの認証トークンを取得する関数
73
- * @param config Firestore設定
74
- * @returns アクセストークン
72
+ * Function to get Firestore authentication token
73
+ * @param config Firestore configuration
74
+ * @returns Access token
75
75
  */
76
76
  async function getFirestoreToken(config) {
77
- // トークンを取得するためのリクエスト
77
+ // No authentication in emulator mode (returns a dummy token)
78
+ if (config.useEmulator) {
79
+ return "firebase-emulator-auth-token";
80
+ }
81
+ // Normal authentication process
78
82
  const response = await fetch("https://oauth2.googleapis.com/token", {
79
83
  method: "POST",
80
84
  headers: {
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 秘密鍵の文字列内にある改行コードのエスケープシーケンスを実際の改行に変換する
3
+ * @param privateKey 変換する秘密鍵文字列
4
+ * @returns 変換後の秘密鍵文字列
5
+ */
6
+ export declare function formatPrivateKey(privateKey: string): string;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.formatPrivateKey = formatPrivateKey;
4
- exports.formatConfig = formatConfig;
5
4
  /**
6
5
  * 秘密鍵の文字列内にある改行コードのエスケープシーケンスを実際の改行に変換する
7
6
  * @param privateKey 変換する秘密鍵文字列
@@ -13,14 +12,3 @@ function formatPrivateKey(privateKey) {
13
12
  }
14
13
  return privateKey;
15
14
  }
16
- /**
17
- * FirestoreConfigオブジェクトの秘密鍵をフォーマットする
18
- * @param config 元のconfigオブジェクト
19
- * @returns 秘密鍵をフォーマットしたconfigオブジェクト
20
- */
21
- function formatConfig(config) {
22
- return {
23
- ...config,
24
- privateKey: formatPrivateKey(config.privateKey),
25
- };
26
- }
@@ -0,0 +1,73 @@
1
+ import { FirestoreConfig } from "../types";
2
+ /**
3
+ * Utility class for constructing Firestore URIs
4
+ * Consistently handles different types of paths and operations
5
+ */
6
+ export declare class FirestorePath {
7
+ private projectId;
8
+ private databaseId;
9
+ private useEmulator;
10
+ private emulatorHost;
11
+ private emulatorPort;
12
+ private debug;
13
+ /**
14
+ * Constructor
15
+ */
16
+ constructor(config: FirestoreConfig, debug?: boolean);
17
+ /**
18
+ * Get Firestore base URL (without document path)
19
+ */
20
+ getBasePath(): string;
21
+ /**
22
+ * Get base URL + collection path for a collection root
23
+ * @param path Collection path (ex: "users" or "users/uid/posts")
24
+ */
25
+ getCollectionPath(path: string): string;
26
+ /**
27
+ * Get the complete URL for a document
28
+ * @param collectionPath Collection path
29
+ * @param documentId Document ID
30
+ */
31
+ getDocumentPath(collectionPath: string, documentId: string): string;
32
+ /**
33
+ * Get URL for query execution
34
+ * @param path Collection path (ex: "users" or "users/uid/posts")
35
+ * @returns URL for query execution, collection ID, and parent path (if needed)
36
+ */
37
+ getQueryPath(path: string): {
38
+ url: string;
39
+ collectionId: string;
40
+ parentPath?: string;
41
+ };
42
+ /**
43
+ * Get reference path for parent document (for query construction)
44
+ * @param parentPath Parent document path
45
+ */
46
+ getParentReference(parentPath: string): string;
47
+ /**
48
+ * Get URL for runQuery
49
+ * @param collectionPath Collection path
50
+ * @returns URL for executing runQuery
51
+ */
52
+ getRunQueryPath(collectionPath: string): string;
53
+ }
54
+ /**
55
+ * Create an instance of FirestorePath class
56
+ * @param config Firestore configuration
57
+ * @param debug Debug mode
58
+ */
59
+ export declare function createFirestorePath(config: FirestoreConfig, debug?: boolean): FirestorePath;
60
+ /**
61
+ * Get Firestore base path URL (without path)
62
+ * @param projectId Project ID
63
+ * @param databaseId Database ID (defaults to default)
64
+ * @param config Firestore configuration (for emulator settings)
65
+ * @returns Firestore base path URL (without path)
66
+ */
67
+ export declare function getFirestoreBasePath(projectId: string, databaseId?: string, config?: FirestoreConfig): string;
68
+ /**
69
+ * Extract document ID from document path
70
+ * @param path Document path
71
+ * @returns Document ID
72
+ */
73
+ export declare function getDocumentId(path: string): string;
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FirestorePath = void 0;
4
+ exports.createFirestorePath = createFirestorePath;
5
+ exports.getFirestoreBasePath = getFirestoreBasePath;
6
+ exports.getDocumentId = getDocumentId;
7
+ /**
8
+ * Utility class for constructing Firestore URIs
9
+ * Consistently handles different types of paths and operations
10
+ */
11
+ class FirestorePath {
12
+ /**
13
+ * Constructor
14
+ */
15
+ constructor(config, debug = false) {
16
+ this.useEmulator = false;
17
+ this.emulatorHost = "localhost";
18
+ this.emulatorPort = 8080;
19
+ this.debug = false;
20
+ this.projectId = config.projectId;
21
+ this.databaseId = config.databaseId || "(default)";
22
+ this.debug = debug;
23
+ if (config.useEmulator) {
24
+ this.useEmulator = true;
25
+ this.emulatorHost = config.emulatorHost || "localhost";
26
+ this.emulatorPort = config.emulatorPort || 8080;
27
+ }
28
+ }
29
+ /**
30
+ * Get Firestore base URL (without document path)
31
+ */
32
+ getBasePath() {
33
+ const baseUrl = this.useEmulator
34
+ ? `http://${this.emulatorHost}:${this.emulatorPort}/v1`
35
+ : "https://firestore.googleapis.com/v1";
36
+ const path = `${baseUrl}/projects/${this.projectId}/databases/${this.databaseId}/documents`;
37
+ if (this.debug) {
38
+ console.log(`Generated base path: ${path}`);
39
+ }
40
+ return path;
41
+ }
42
+ /**
43
+ * Get base URL + collection path for a collection root
44
+ * @param path Collection path (ex: "users" or "users/uid/posts")
45
+ */
46
+ getCollectionPath(path) {
47
+ // Remove leading and trailing slashes
48
+ const cleanPath = path.replace(/^\/+|\/+$/g, '');
49
+ const fullPath = `${this.getBasePath()}/${cleanPath}`;
50
+ if (this.debug) {
51
+ console.log(`Generated collection path: ${fullPath}`);
52
+ }
53
+ return fullPath;
54
+ }
55
+ /**
56
+ * Get the complete URL for a document
57
+ * @param collectionPath Collection path
58
+ * @param documentId Document ID
59
+ */
60
+ getDocumentPath(collectionPath, documentId) {
61
+ const cleanCollectionPath = collectionPath.replace(/^\/+|\/+$/g, '');
62
+ const path = `${this.getBasePath()}/${cleanCollectionPath}/${documentId}`;
63
+ if (this.debug) {
64
+ console.log(`Generated document path: ${path}`);
65
+ }
66
+ return path;
67
+ }
68
+ /**
69
+ * Get URL for query execution
70
+ * @param path Collection path (ex: "users" or "users/uid/posts")
71
+ * @returns URL for query execution, collection ID, and parent path (if needed)
72
+ */
73
+ getQueryPath(path) {
74
+ // パスをセグメントに分割
75
+ const segments = path.replace(/^\/+|\/+$/g, '').split('/');
76
+ // 単一コレクションの場合
77
+ if (segments.length === 1) {
78
+ const url = `${this.getBasePath()}:runQuery`;
79
+ if (this.debug) {
80
+ console.log(`Generated query URL (single collection): ${url}`);
81
+ console.log(`Collection ID: ${segments[0]}`);
82
+ }
83
+ return {
84
+ url,
85
+ collectionId: segments[0]
86
+ };
87
+ }
88
+ // ネストしたコレクションパスの場合 (例: "users/uid/posts")
89
+ const collectionId = segments[segments.length - 1];
90
+ const parentSegments = segments.slice(0, -1);
91
+ const parentPath = parentSegments.join('/');
92
+ // ベースURLでネストしたドキュメントまでのパスを取得
93
+ const url = `${this.getBasePath()}:runQuery`;
94
+ if (this.debug) {
95
+ console.log(`Generated query URL (nested collection): ${url}`);
96
+ console.log(`Collection ID: ${collectionId}`);
97
+ console.log(`Parent path: ${parentPath}`);
98
+ }
99
+ return {
100
+ url,
101
+ collectionId,
102
+ parentPath
103
+ };
104
+ }
105
+ /**
106
+ * Get reference path for parent document (for query construction)
107
+ * @param parentPath Parent document path
108
+ */
109
+ getParentReference(parentPath) {
110
+ return `projects/${this.projectId}/databases/${this.databaseId}/documents/${parentPath}`;
111
+ }
112
+ /**
113
+ * Get URL for runQuery
114
+ * @param collectionPath Collection path
115
+ * @returns URL for executing runQuery
116
+ */
117
+ getRunQueryPath(collectionPath) {
118
+ // コレクションパス情報を取得
119
+ const { collectionId, parentPath } = this.getQueryPath(collectionPath);
120
+ // parentPathがある場合は、親ドキュメントパスを使用してURLを作成
121
+ if (parentPath) {
122
+ // getBasePathからベースURLを取得
123
+ const baseUrl = this.getBasePath().replace(/\/documents$/, '');
124
+ // 親ドキュメントパスを含むrunQueryのURL
125
+ const runQueryUrl = `${baseUrl}/documents/${parentPath}:runQuery`;
126
+ if (this.debug) {
127
+ console.log(`Generated runQuery URL for nested collection: ${runQueryUrl}`);
128
+ console.log(`Collection ID: ${collectionId}`);
129
+ }
130
+ return runQueryUrl;
131
+ }
132
+ // トップレベルコレクションの場合は、ベースパスを使用
133
+ const baseUrl = this.getBasePath();
134
+ const runQueryUrl = `${baseUrl}:runQuery`;
135
+ if (this.debug) {
136
+ console.log(`Generated runQuery URL for top-level collection: ${runQueryUrl}`);
137
+ console.log(`Collection ID: ${collectionId}`);
138
+ }
139
+ return runQueryUrl;
140
+ }
141
+ }
142
+ exports.FirestorePath = FirestorePath;
143
+ /**
144
+ * Create an instance of FirestorePath class
145
+ * @param config Firestore configuration
146
+ * @param debug Debug mode
147
+ */
148
+ function createFirestorePath(config, debug = false) {
149
+ return new FirestorePath(config, debug);
150
+ }
151
+ /**
152
+ * Get Firestore base path URL (without path)
153
+ * @param projectId Project ID
154
+ * @param databaseId Database ID (defaults to default)
155
+ * @param config Firestore configuration (for emulator settings)
156
+ * @returns Firestore base path URL (without path)
157
+ */
158
+ function getFirestoreBasePath(projectId, databaseId, config) {
159
+ // Use emulator URL for emulator mode
160
+ if (config?.useEmulator) {
161
+ const host = config.emulatorHost || "127.0.0.1";
162
+ const port = config.emulatorPort || 8080;
163
+ return `http://${host}:${port}/v1/projects/${projectId}/databases/${databaseId || "(default)"}/documents`;
164
+ }
165
+ // Use normal production environment URL
166
+ return `https://firestore.googleapis.com/v1/projects/${projectId}/databases/${databaseId || "(default)"}/documents`;
167
+ }
168
+ /**
169
+ * Extract document ID from document path
170
+ * @param path Document path
171
+ * @returns Document ID
172
+ */
173
+ function getDocumentId(path) {
174
+ const parts = path.split("/");
175
+ return parts[parts.length - 1];
176
+ }