firebase-rest-firestore 1.2.0 → 1.6.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 (52) hide show
  1. package/README.md +37 -0
  2. package/dist/cjs/client.d.ts +454 -0
  3. package/dist/cjs/client.js +1192 -0
  4. package/dist/cjs/field-value.d.ts +22 -0
  5. package/dist/cjs/field-value.js +31 -0
  6. package/dist/{index.d.ts → cjs/index.d.ts} +2 -1
  7. package/dist/{index.js → cjs/index.js} +4 -2
  8. package/dist/cjs/types.d.ts +146 -0
  9. package/dist/cjs/types.js +75 -0
  10. package/dist/cjs/utils/auth.d.ts +13 -0
  11. package/dist/{utils → cjs/utils}/auth.js +14 -10
  12. package/dist/cjs/utils/config.d.ts +6 -0
  13. package/dist/{utils → cjs/utils}/config.js +0 -12
  14. package/dist/cjs/utils/converter.d.ts +57 -0
  15. package/dist/cjs/utils/converter.js +224 -0
  16. package/dist/cjs/utils/path.d.ts +73 -0
  17. package/dist/cjs/utils/path.js +176 -0
  18. package/dist/esm/client.d.ts +454 -0
  19. package/dist/esm/client.js +1180 -0
  20. package/dist/esm/field-value.d.ts +22 -0
  21. package/dist/esm/field-value.js +27 -0
  22. package/dist/esm/index.d.ts +8 -0
  23. package/dist/esm/index.js +13 -0
  24. package/dist/esm/types.d.ts +146 -0
  25. package/dist/esm/types.js +70 -0
  26. package/dist/esm/utils/auth.d.ts +13 -0
  27. package/dist/esm/utils/auth.js +57 -0
  28. package/dist/esm/utils/config.d.ts +6 -0
  29. package/dist/esm/utils/config.js +11 -0
  30. package/dist/esm/utils/converter.d.ts +57 -0
  31. package/dist/esm/utils/converter.js +216 -0
  32. package/dist/esm/utils/path.d.ts +73 -0
  33. package/dist/esm/utils/path.js +169 -0
  34. package/dist/types/client.d.ts +454 -0
  35. package/dist/types/field-value.d.ts +22 -0
  36. package/dist/types/index.d.ts +8 -0
  37. package/dist/types/types.d.ts +146 -0
  38. package/dist/types/utils/auth.d.ts +13 -0
  39. package/dist/types/utils/config.d.ts +6 -0
  40. package/dist/types/utils/converter.d.ts +57 -0
  41. package/dist/types/utils/path.d.ts +73 -0
  42. package/package.json +19 -5
  43. package/dist/client.d.ts +0 -381
  44. package/dist/client.js +0 -915
  45. package/dist/types.d.ts +0 -65
  46. package/dist/types.js +0 -2
  47. package/dist/utils/auth.d.ts +0 -13
  48. package/dist/utils/config.d.ts +0 -13
  49. package/dist/utils/converter.d.ts +0 -27
  50. package/dist/utils/converter.js +0 -113
  51. package/dist/utils/path.d.ts +0 -14
  52. package/dist/utils/path.js +0 -27
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Sentinel values for special write behaviors, mirroring the native Firebase
3
+ * SDK's `FieldValue`. Currently only `serverTimestamp()` is supported.
4
+ *
5
+ * A `FieldValue` is not a real field value: when used as a field in a write it
6
+ * is translated into a Firestore field transform (applied server-side) rather
7
+ * than serialized as data. Using one anywhere else (e.g. inside an array) is an
8
+ * error.
9
+ */
10
+ export declare class FieldValue {
11
+ readonly methodName: "serverTimestamp";
12
+ private constructor();
13
+ /**
14
+ * Returns a sentinel that sets the field to the server's request timestamp at
15
+ * write time, e.g. `client.add("posts", { createdAt: FieldValue.serverTimestamp() })`.
16
+ */
17
+ static serverTimestamp(): FieldValue;
18
+ /**
19
+ * Whether this sentinel represents the same transform as another.
20
+ */
21
+ isEqual(other: FieldValue): boolean;
22
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FieldValue = void 0;
4
+ /**
5
+ * Sentinel values for special write behaviors, mirroring the native Firebase
6
+ * SDK's `FieldValue`. Currently only `serverTimestamp()` is supported.
7
+ *
8
+ * A `FieldValue` is not a real field value: when used as a field in a write it
9
+ * is translated into a Firestore field transform (applied server-side) rather
10
+ * than serialized as data. Using one anywhere else (e.g. inside an array) is an
11
+ * error.
12
+ */
13
+ class FieldValue {
14
+ constructor(methodName) {
15
+ this.methodName = methodName;
16
+ }
17
+ /**
18
+ * Returns a sentinel that sets the field to the server's request timestamp at
19
+ * write time, e.g. `client.add("posts", { createdAt: FieldValue.serverTimestamp() })`.
20
+ */
21
+ static serverTimestamp() {
22
+ return new FieldValue("serverTimestamp");
23
+ }
24
+ /**
25
+ * Whether this sentinel represents the same transform as another.
26
+ */
27
+ isEqual(other) {
28
+ return other instanceof FieldValue && other.methodName === this.methodName;
29
+ }
30
+ }
31
+ exports.FieldValue = FieldValue;
@@ -1,7 +1,8 @@
1
1
  export * from "./types";
2
2
  import { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult } from "./client";
3
+ export { FieldValue } from "./field-value";
3
4
  export { getFirestoreToken } from "./utils/auth";
4
5
  export { convertToFirestoreValue, convertFromFirestoreValue, convertToFirestoreDocument, convertFromFirestoreDocument, } from "./utils/converter";
5
6
  export { getFirestoreBasePath, getDocumentId } from "./utils/path";
6
- export { formatPrivateKey, formatConfig } from "./utils/config";
7
+ export { formatPrivateKey } from "./utils/config";
7
8
  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 = exports.FieldValue = void 0;
18
18
  // 型定義のエクスポート
19
19
  __exportStar(require("./types"), exports);
20
20
  // クライアントのエクスポート
@@ -28,6 +28,9 @@ Object.defineProperty(exports, "Query", { enumerable: true, get: function () { r
28
28
  Object.defineProperty(exports, "QuerySnapshot", { enumerable: true, get: function () { return client_1.QuerySnapshot; } });
29
29
  Object.defineProperty(exports, "DocumentSnapshot", { enumerable: true, get: function () { return client_1.DocumentSnapshot; } });
30
30
  Object.defineProperty(exports, "WriteResult", { enumerable: true, get: function () { return client_1.WriteResult; } });
31
+ // FieldValue センチネルのエクスポート
32
+ var field_value_1 = require("./field-value");
33
+ Object.defineProperty(exports, "FieldValue", { enumerable: true, get: function () { return field_value_1.FieldValue; } });
31
34
  // ユーティリティ関数のエクスポート
32
35
  var auth_1 = require("./utils/auth");
33
36
  Object.defineProperty(exports, "getFirestoreToken", { enumerable: true, get: function () { return auth_1.getFirestoreToken; } });
@@ -41,4 +44,3 @@ Object.defineProperty(exports, "getFirestoreBasePath", { enumerable: true, get:
41
44
  Object.defineProperty(exports, "getDocumentId", { enumerable: true, get: function () { return path_1.getDocumentId; } });
42
45
  var config_1 = require("./utils/config");
43
46
  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; } });
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Firestoreクライアントの設定インターフェース
3
+ */
4
+ export interface FirestoreConfig {
5
+ projectId: string;
6
+ privateKey: string;
7
+ clientEmail: string;
8
+ databaseId?: string;
9
+ debug?: boolean;
10
+ useEmulator?: boolean;
11
+ emulatorHost?: string;
12
+ emulatorPort?: number;
13
+ }
14
+ /**
15
+ * A reference to a document. For example: `projects/{project_id}/databases/{database_id}/documents/{document_path}`.
16
+ * Used to represent document references globally and not connected to any particular client.
17
+ */
18
+ export declare class LiteralDocumentReference {
19
+ referenceValue: string;
20
+ constructor(options: Pick<LiteralDocumentReference, "referenceValue">);
21
+ /**
22
+ * Globally unique Firestore document reference paths look like:
23
+ * projects/{project_id}/databases/{database_id}/documents/{document_path}
24
+ * The database id (e.g. `(default)`) never contains a slash, while the
25
+ * document path may contain many. A single anchored regex parses this
26
+ * without pulling in a URLPattern polyfill.
27
+ */
28
+ private static readonly pattern;
29
+ private parse;
30
+ /**
31
+ * Get Project ID
32
+ */
33
+ get project_id(): string;
34
+ /**
35
+ * Get Database ID
36
+ * Ex: `(default)`
37
+ */
38
+ get database_id(): string;
39
+ /**
40
+ * Get document ID
41
+ */
42
+ get id(): string;
43
+ /**
44
+ * Get the collection ID
45
+ */
46
+ get collectionPath(): string;
47
+ /**
48
+ * Get document path
49
+ */
50
+ get path(): string;
51
+ }
52
+ /**
53
+ * A geo point value representing a point on the surface of Earth.
54
+ */
55
+ export declare class LiteralGeoPointValue {
56
+ geoPointValue: {
57
+ /**
58
+ * The latitude in degrees. It must be in the range [-90.0, +90.0].
59
+ */
60
+ latitude: number;
61
+ /**
62
+ * The longitude in degrees. It must be in the range [-180.0, +180.0].
63
+ */
64
+ longitude: number;
65
+ };
66
+ constructor(options: Pick<LiteralGeoPointValue, "geoPointValue">);
67
+ }
68
+ /**
69
+ * Firestoreの値型定義
70
+ * See: https://github.com/googleapis/google-api-nodejs-client/blob/5870dfe31f4885eebc82c19f7471c50403308f26/src/apis/firestore/v1.ts#L2246
71
+ */
72
+ export type FirestoreFieldValue = {
73
+ stringValue: string;
74
+ } | {
75
+ integerValue: number;
76
+ } | {
77
+ doubleValue: number;
78
+ } | {
79
+ booleanValue: boolean;
80
+ } | {
81
+ nullValue: null;
82
+ } | {
83
+ timestampValue: string;
84
+ } | Pick<LiteralGeoPointValue, 'geoPointValue'> | Pick<LiteralDocumentReference, 'referenceValue'> | {
85
+ mapValue: {
86
+ fields: Record<string, FirestoreFieldValue>;
87
+ };
88
+ } | {
89
+ arrayValue: {
90
+ values: FirestoreFieldValue[];
91
+ };
92
+ };
93
+ /**
94
+ * A Firestore field transform applied server-side during a commit write.
95
+ * See: https://firebase.google.com/docs/firestore/reference/rest/v1/Write#FieldTransform
96
+ */
97
+ export interface FieldTransform {
98
+ fieldPath: string;
99
+ setToServerValue: "REQUEST_TIME";
100
+ }
101
+ /**
102
+ * A single write in a `documents:commit` request.
103
+ */
104
+ export interface CommitWrite {
105
+ update: {
106
+ name: string;
107
+ fields: Record<string, FirestoreFieldValue>;
108
+ };
109
+ updateTransforms?: FieldTransform[];
110
+ currentDocument?: {
111
+ exists?: boolean;
112
+ updateTime?: string;
113
+ };
114
+ }
115
+ /**
116
+ * Firestoreドキュメント型
117
+ */
118
+ export interface FirestoreDocument {
119
+ name?: string;
120
+ fields: Record<string, FirestoreFieldValue>;
121
+ createTime?: string;
122
+ updateTime?: string;
123
+ }
124
+ /**
125
+ * Firestoreレスポンス型
126
+ */
127
+ export interface FirestoreResponse {
128
+ name: string;
129
+ fields?: Record<string, FirestoreFieldValue>;
130
+ createTime?: string;
131
+ updateTime?: string;
132
+ }
133
+ /**
134
+ * クエリオプション型
135
+ */
136
+ export interface QueryOptions {
137
+ where?: Array<{
138
+ field: string;
139
+ op: string;
140
+ value: any;
141
+ }>;
142
+ orderBy?: string;
143
+ orderDirection?: string;
144
+ limit?: number;
145
+ offset?: number;
146
+ }
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LiteralGeoPointValue = exports.LiteralDocumentReference = void 0;
4
+ /**
5
+ * A reference to a document. For example: `projects/{project_id}/databases/{database_id}/documents/{document_path}`.
6
+ * Used to represent document references globally and not connected to any particular client.
7
+ */
8
+ class LiteralDocumentReference {
9
+ constructor(options) {
10
+ this.referenceValue = options.referenceValue;
11
+ }
12
+ parse() {
13
+ const match = LiteralDocumentReference.pattern.exec(this.referenceValue);
14
+ if (!match) {
15
+ throw new Error("Invalid document path. Path does not match pattern.");
16
+ }
17
+ const [, project_id, database_id, document_path] = match;
18
+ return { project_id, database_id, document_path };
19
+ }
20
+ /**
21
+ * Get Project ID
22
+ */
23
+ get project_id() {
24
+ return this.parse().project_id;
25
+ }
26
+ /**
27
+ * Get Database ID
28
+ * Ex: `(default)`
29
+ */
30
+ get database_id() {
31
+ return this.parse().database_id;
32
+ }
33
+ /**
34
+ * Get document ID
35
+ */
36
+ get id() {
37
+ const path = this.parse().document_path;
38
+ const parts = path.split("/");
39
+ const docId = parts[parts.length - 1];
40
+ return docId;
41
+ }
42
+ /**
43
+ * Get the collection ID
44
+ */
45
+ get collectionPath() {
46
+ const path = this.parse().document_path;
47
+ const parts = path.split("/");
48
+ const collectionPath = parts.slice(0, parts.length - 1).join("/");
49
+ return collectionPath;
50
+ }
51
+ /**
52
+ * Get document path
53
+ */
54
+ get path() {
55
+ return this.parse().document_path;
56
+ }
57
+ }
58
+ exports.LiteralDocumentReference = LiteralDocumentReference;
59
+ /**
60
+ * Globally unique Firestore document reference paths look like:
61
+ * projects/{project_id}/databases/{database_id}/documents/{document_path}
62
+ * The database id (e.g. `(default)`) never contains a slash, while the
63
+ * document path may contain many. A single anchored regex parses this
64
+ * without pulling in a URLPattern polyfill.
65
+ */
66
+ LiteralDocumentReference.pattern = /^projects\/([^/]+)\/databases\/([^/]+)\/documents\/(.+)$/;
67
+ /**
68
+ * A geo point value representing a point on the surface of Earth.
69
+ */
70
+ class LiteralGeoPointValue {
71
+ constructor(options) {
72
+ this.geoPointValue = options.geoPointValue;
73
+ }
74
+ }
75
+ exports.LiteralGeoPointValue = LiteralGeoPointValue;
@@ -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,57 @@
1
+ import { CommitWrite, FieldTransform, FirestoreDocument, FirestoreFieldValue, FirestoreResponse } from "../types";
2
+ /**
3
+ * JSの値をFirestore形式に変換する
4
+ * @param value 変換する値
5
+ * @returns Firestore形式の値
6
+ */
7
+ export declare function convertToFirestoreValue(value: any): FirestoreFieldValue;
8
+ /**
9
+ * Firestore形式からJSの値に変換する
10
+ * @param firestoreValue Firestore形式の値
11
+ * @returns JS形式の値
12
+ */
13
+ export declare function convertFromFirestoreValue(firestoreValue: FirestoreFieldValue): any;
14
+ /**
15
+ * オブジェクトをFirestoreドキュメント形式に変換
16
+ * @param data 変換するオブジェクト
17
+ * @returns Firestoreドキュメント
18
+ */
19
+ export declare function convertToFirestoreDocument(data: Record<string, any>): FirestoreDocument;
20
+ /**
21
+ * Split write data into plain field values and Firestore field transforms.
22
+ *
23
+ * `FieldValue` sentinels (e.g. `serverTimestamp()`) are pulled out into
24
+ * transforms keyed by their (escaped, dot-separated) field path; everything
25
+ * else is left untouched in `fields`. Recursion only descends into plain
26
+ * objects, so class instances (Date / references / geo points) are treated as
27
+ * leaves.
28
+ *
29
+ * @param data Write data (JS values, may contain FieldValue sentinels)
30
+ * @param prefix Field-path prefix used while recursing (internal, pre-escaped)
31
+ */
32
+ export declare function extractFieldTransforms(data: Record<string, any>, prefix?: string): {
33
+ fields: Record<string, any>;
34
+ transforms: FieldTransform[];
35
+ };
36
+ /**
37
+ * Build a single `documents:commit` write that updates a document and applies
38
+ * field transforms. `updateTransforms` / `currentDocument` are only included
39
+ * when relevant.
40
+ *
41
+ * @param documentName Full resource name (projects/.../documents/<path>)
42
+ * @param fields Already-converted Firestore field values
43
+ * @param transforms Field transforms to apply after the update
44
+ * @param currentDocument Optional precondition (e.g. `{ exists: false }`)
45
+ */
46
+ export declare function buildCommitWrite(documentName: string, fields: Record<string, FirestoreFieldValue>, transforms: FieldTransform[], currentDocument?: {
47
+ exists?: boolean;
48
+ updateTime?: string;
49
+ }): CommitWrite;
50
+ /**
51
+ * Firestoreドキュメントをオブジェクトに変換
52
+ * @param doc Firestoreレスポンス
53
+ * @returns 変換されたオブジェクト(idプロパティ付き)
54
+ */
55
+ export declare function convertFromFirestoreDocument(doc: FirestoreResponse): Record<string, any> & {
56
+ id: string;
57
+ };