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,8 @@
1
+ export * from "./types";
2
+ import { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult } from "./client";
3
+ export { FieldValue } from "./field-value";
4
+ export { getFirestoreToken } from "./utils/auth";
5
+ export { convertToFirestoreValue, convertFromFirestoreValue, convertToFirestoreDocument, convertFromFirestoreDocument, } from "./utils/converter";
6
+ export { getFirestoreBasePath, getDocumentId } from "./utils/path";
7
+ export { formatPrivateKey } from "./utils/config";
8
+ export { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult, };
@@ -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,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>;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 秘密鍵の文字列内にある改行コードのエスケープシーケンスを実際の改行に変換する
3
+ * @param privateKey 変換する秘密鍵文字列
4
+ * @returns 変換後の秘密鍵文字列
5
+ */
6
+ export declare function formatPrivateKey(privateKey: string): string;
@@ -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
+ };
@@ -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;
package/package.json CHANGED
@@ -1,17 +1,30 @@
1
1
  {
2
2
  "name": "firebase-rest-firestore",
3
- "version": "1.2.0",
3
+ "version": "1.6.0",
4
4
  "description": "Firebase Firestore REST API client for Edge runtime environments",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
5
+ "main": "dist/cjs/index.js",
6
+ "module": "dist/esm/index.js",
7
+ "types": "dist/types/index.d.ts",
7
8
  "files": [
8
9
  "dist",
9
10
  "README.md"
10
11
  ],
11
12
  "scripts": {
12
- "build": "tsc",
13
+ "build": "npm run build:esm && npm run build:cjs && npm run build:types",
14
+ "build:esm": "tsc -p tsconfig.esm.json",
15
+ "build:cjs": "tsc -p tsconfig.cjs.json",
16
+ "build:types": "tsc -p tsconfig.json --emitDeclarationOnly --declarationDir dist/types",
17
+ "watch": "concurrently \"npm run watch:esm\" \"npm run watch:cjs\" \"npm run watch:types\"",
18
+ "watch:esm": "tsc -p tsconfig.esm.json --watch",
19
+ "watch:cjs": "tsc -p tsconfig.cjs.json --watch",
20
+ "watch:types": "tsc -p tsconfig.json --emitDeclarationOnly --declarationDir dist/types --watch",
13
21
  "prepublishOnly": "npm run build",
14
- "test": "vitest"
22
+ "setup:local:env": "cp .env.local.example .env && echo 'Created .env file from local example.'",
23
+ "emulator:start": "cd test/emulator && firebase emulators:start -P demo-test-project",
24
+ "emulator:stop": "npx kill-port -y 4089 8089 9089",
25
+ "test": "vitest",
26
+ "test:unit": "vitest run test/converter.test.ts test/references.test.ts test/field-value.test.ts",
27
+ "test:emulator": "bash test/scripts/test-with-emulator.sh"
15
28
  },
16
29
  "keywords": [
17
30
  "firebase",
@@ -32,6 +45,7 @@
32
45
  "@semantic-release/changelog": "^6.0.3",
33
46
  "@semantic-release/git": "^10.0.1",
34
47
  "@types/node": "^18.16.0",
48
+ "concurrently": "^8.2.2",
35
49
  "dotenv": "^16.4.7",
36
50
  "semantic-release": "^24.2.3",
37
51
  "typescript": "^5.0.4",