@cougargrades/firebase-rest-firestore 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.
@@ -0,0 +1,7 @@
1
+ export * from "./types";
2
+ import { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult } from "./client";
3
+ export { getFirestoreToken } from "./utils/auth";
4
+ export { convertToFirestoreValue, convertFromFirestoreValue, convertToFirestoreDocument, convertFromFirestoreDocument, } from "./utils/converter";
5
+ export { getFirestoreBasePath, getDocumentId } from "./utils/path";
6
+ export { formatPrivateKey } from "./utils/config";
7
+ export { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult, };
@@ -0,0 +1,11 @@
1
+ // 型定義のエクスポート
2
+ export * from "./types";
3
+ // クライアントのエクスポート
4
+ import { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult, } from "./client";
5
+ // ユーティリティ関数のエクスポート
6
+ export { getFirestoreToken } from "./utils/auth";
7
+ export { convertToFirestoreValue, convertFromFirestoreValue, convertToFirestoreDocument, convertFromFirestoreDocument, } from "./utils/converter";
8
+ export { getFirestoreBasePath, getDocumentId } from "./utils/path";
9
+ export { formatPrivateKey } from "./utils/config";
10
+ // クライアント関連のエクスポート
11
+ export { FirestoreClient, createFirestoreClient, CollectionReference, DocumentReference, CollectionGroup, Query, QuerySnapshot, DocumentSnapshot, WriteResult, };
@@ -0,0 +1,127 @@
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
+ * The pattern for globally unique Firestore Document Reference paths
23
+ */
24
+ private static pattern;
25
+ /**
26
+ * Parse using URLPattern, which is a relatively new addition to the standard.
27
+ * See: https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol
28
+ * Added in Node.js v23: https://nodejs.org/api/url.html#class-urlpattern
29
+ * Baseline 2025 in browsers: https://developer.mozilla.org/en-US/docs/Web/API/URLPattern/protocol
30
+ * @returns
31
+ */
32
+ private parse;
33
+ /**
34
+ * Get Project ID
35
+ */
36
+ get project_id(): string;
37
+ /**
38
+ * Get Database ID
39
+ * Ex: `(default)`
40
+ */
41
+ get database_id(): string;
42
+ /**
43
+ * Get document ID
44
+ */
45
+ get id(): string;
46
+ /**
47
+ * Get the collection ID
48
+ */
49
+ get collectionPath(): string;
50
+ /**
51
+ * Get document path
52
+ */
53
+ get path(): string;
54
+ }
55
+ /**
56
+ * A geo point value representing a point on the surface of Earth.
57
+ */
58
+ export declare class LiteralGeoPointValue {
59
+ geoPointValue: {
60
+ /**
61
+ * The latitude in degrees. It must be in the range [-90.0, +90.0].
62
+ */
63
+ latitude: number;
64
+ /**
65
+ * The longitude in degrees. It must be in the range [-180.0, +180.0].
66
+ */
67
+ longitude: number;
68
+ };
69
+ constructor(options: Pick<LiteralGeoPointValue, "geoPointValue">);
70
+ }
71
+ /**
72
+ * Firestoreの値型定義
73
+ * See: https://github.com/googleapis/google-api-nodejs-client/blob/5870dfe31f4885eebc82c19f7471c50403308f26/src/apis/firestore/v1.ts#L2246
74
+ */
75
+ export type FirestoreFieldValue = {
76
+ stringValue: string;
77
+ } | {
78
+ integerValue: number;
79
+ } | {
80
+ doubleValue: number;
81
+ } | {
82
+ booleanValue: boolean;
83
+ } | {
84
+ nullValue: null;
85
+ } | {
86
+ timestampValue: string;
87
+ } | Pick<LiteralGeoPointValue, 'geoPointValue'> | Pick<LiteralDocumentReference, 'referenceValue'> | {
88
+ mapValue: {
89
+ fields: Record<string, FirestoreFieldValue>;
90
+ };
91
+ } | {
92
+ arrayValue: {
93
+ values: FirestoreFieldValue[];
94
+ };
95
+ };
96
+ /**
97
+ * Firestoreドキュメント型
98
+ */
99
+ export interface FirestoreDocument {
100
+ name?: string;
101
+ fields: Record<string, FirestoreFieldValue>;
102
+ createTime?: string;
103
+ updateTime?: string;
104
+ }
105
+ /**
106
+ * Firestoreレスポンス型
107
+ */
108
+ export interface FirestoreResponse {
109
+ name: string;
110
+ fields?: Record<string, FirestoreFieldValue>;
111
+ createTime?: string;
112
+ updateTime?: string;
113
+ }
114
+ /**
115
+ * クエリオプション型
116
+ */
117
+ export interface QueryOptions {
118
+ where?: Array<{
119
+ field: string;
120
+ op: string;
121
+ value: any;
122
+ }>;
123
+ orderBy?: string;
124
+ orderDirection?: string;
125
+ limit?: number;
126
+ offset?: number;
127
+ }
@@ -0,0 +1,81 @@
1
+ import { URLPattern } from "urlpattern-polyfill";
2
+ /**
3
+ * A reference to a document. For example: `projects/{project_id}/databases/{database_id}/documents/{document_path}`.
4
+ * Used to represent document references globally and not connected to any particular client.
5
+ */
6
+ export class LiteralDocumentReference {
7
+ constructor(options) {
8
+ this.referenceValue = options.referenceValue;
9
+ }
10
+ /**
11
+ * Parse using URLPattern, which is a relatively new addition to the standard.
12
+ * See: https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol
13
+ * Added in Node.js v23: https://nodejs.org/api/url.html#class-urlpattern
14
+ * Baseline 2025 in browsers: https://developer.mozilla.org/en-US/docs/Web/API/URLPattern/protocol
15
+ * @returns
16
+ */
17
+ parse() {
18
+ const result = LiteralDocumentReference.pattern.exec(`https://hostname/${this.referenceValue}`);
19
+ if (!result)
20
+ throw new Error("Invalid document path. Path does not match pattern.");
21
+ const project_id = result.pathname.groups["project_id"];
22
+ if (!project_id)
23
+ throw new Error("Invalid document path. Path does not match pattern.");
24
+ const database_id = result.pathname.groups["database_id"];
25
+ if (!database_id)
26
+ throw new Error("Invalid document path. Path does not match pattern.");
27
+ const document_path = result.pathname.groups["document_path"];
28
+ if (!document_path)
29
+ throw new Error("Invalid document path. Path does not match pattern.");
30
+ return { project_id, database_id, document_path };
31
+ }
32
+ /**
33
+ * Get Project ID
34
+ */
35
+ get project_id() {
36
+ return this.parse().project_id;
37
+ }
38
+ /**
39
+ * Get Database ID
40
+ * Ex: `(default)`
41
+ */
42
+ get database_id() {
43
+ return this.parse().database_id;
44
+ }
45
+ /**
46
+ * Get document ID
47
+ */
48
+ get id() {
49
+ const path = this.parse().document_path;
50
+ const parts = path.split("/");
51
+ const docId = parts[parts.length - 1];
52
+ return docId;
53
+ }
54
+ /**
55
+ * Get the collection ID
56
+ */
57
+ get collectionPath() {
58
+ const path = this.parse().document_path;
59
+ const parts = path.split("/");
60
+ const collectionPath = parts.slice(0, parts.length - 1).join("/");
61
+ return collectionPath;
62
+ }
63
+ /**
64
+ * Get document path
65
+ */
66
+ get path() {
67
+ return this.parse().document_path;
68
+ }
69
+ }
70
+ /**
71
+ * The pattern for globally unique Firestore Document Reference paths
72
+ */
73
+ LiteralDocumentReference.pattern = new URLPattern({ pathname: `/projects/:project_id/databases/:database_id/documents/:document_path*` });
74
+ /**
75
+ * A geo point value representing a point on the surface of Earth.
76
+ */
77
+ export class LiteralGeoPointValue {
78
+ constructor(options) {
79
+ this.geoPointValue = options.geoPointValue;
80
+ }
81
+ }
@@ -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,57 @@
1
+ import * as jose from "jose";
2
+ /**
3
+ * Function to create a JWT (JSON Web Token)
4
+ * @param config Firestore configuration
5
+ * @returns JWT string
6
+ */
7
+ export async function createJWT(config) {
8
+ const now = Math.floor(Date.now() / 1000);
9
+ const payload = {
10
+ iss: config.clientEmail,
11
+ sub: config.clientEmail,
12
+ aud: "https://oauth2.googleapis.com/token",
13
+ iat: now,
14
+ exp: now + 3600, // Expires in 1 hour
15
+ scope: "https://www.googleapis.com/auth/datastore",
16
+ };
17
+ try {
18
+ // Import the private key
19
+ const privateKey = await jose.importPKCS8(config.privateKey, "RS256");
20
+ // Create JWT
21
+ const token = await new jose.SignJWT(payload)
22
+ .setProtectedHeader({
23
+ alg: "RS256",
24
+ typ: "JWT",
25
+ })
26
+ .sign(privateKey);
27
+ return token;
28
+ }
29
+ catch (error) {
30
+ console.error("Error creating JWT:", error);
31
+ throw error;
32
+ }
33
+ }
34
+ /**
35
+ * Function to get Firestore authentication token
36
+ * @param config Firestore configuration
37
+ * @returns Access token
38
+ */
39
+ export async function getFirestoreToken(config) {
40
+ // No authentication in emulator mode (returns a dummy token)
41
+ if (config.useEmulator) {
42
+ return "firebase-emulator-auth-token";
43
+ }
44
+ // Normal authentication process
45
+ const response = await fetch("https://oauth2.googleapis.com/token", {
46
+ method: "POST",
47
+ headers: {
48
+ "Content-Type": "application/json",
49
+ },
50
+ body: JSON.stringify({
51
+ grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
52
+ assertion: await createJWT(config),
53
+ }),
54
+ });
55
+ const data = (await response.json());
56
+ return data.access_token;
57
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * 秘密鍵の文字列内にある改行コードのエスケープシーケンスを実際の改行に変換する
3
+ * @param privateKey 変換する秘密鍵文字列
4
+ * @returns 変換後の秘密鍵文字列
5
+ */
6
+ export declare function formatPrivateKey(privateKey: string): string;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * 秘密鍵の文字列内にある改行コードのエスケープシーケンスを実際の改行に変換する
3
+ * @param privateKey 変換する秘密鍵文字列
4
+ * @returns 変換後の秘密鍵文字列
5
+ */
6
+ export function formatPrivateKey(privateKey) {
7
+ if (privateKey.includes("\\n")) {
8
+ return privateKey.replace(/\\n/g, "\n");
9
+ }
10
+ return privateKey;
11
+ }
@@ -0,0 +1,27 @@
1
+ import { 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
+ * Firestoreドキュメントをオブジェクトに変換
22
+ * @param doc Firestoreレスポンス
23
+ * @returns 変換されたオブジェクト(idプロパティ付き)
24
+ */
25
+ export declare function convertFromFirestoreDocument(doc: FirestoreResponse): Record<string, any> & {
26
+ id: string;
27
+ };
@@ -0,0 +1,126 @@
1
+ import { DocumentReference } from "../client";
2
+ import { LiteralDocumentReference, LiteralGeoPointValue, } from "../types";
3
+ import { getDocumentId } from "./path";
4
+ /**
5
+ * JSの値をFirestore形式に変換する
6
+ * @param value 変換する値
7
+ * @returns Firestore形式の値
8
+ */
9
+ export function convertToFirestoreValue(value) {
10
+ // store in temporary variable to enable TypeScript to be more thorough
11
+ let unknown = value;
12
+ if (value instanceof Date) {
13
+ return { timestampValue: value.toISOString() };
14
+ }
15
+ else if (value instanceof DocumentReference) {
16
+ return { referenceValue: value["client"]["pathUtil"].getParentReference(value.path) };
17
+ }
18
+ else if (value instanceof LiteralDocumentReference) {
19
+ return { referenceValue: value.referenceValue };
20
+ }
21
+ else if (value instanceof LiteralGeoPointValue) {
22
+ return { geoPointValue: value.geoPointValue };
23
+ }
24
+ else if (typeof value === "string") {
25
+ return { stringValue: value };
26
+ }
27
+ else if (typeof value === "number") {
28
+ return Number.isInteger(value)
29
+ ? { integerValue: value }
30
+ : { doubleValue: value };
31
+ }
32
+ else if (typeof value === "boolean") {
33
+ return { booleanValue: value };
34
+ }
35
+ else if (value === null || value === undefined) {
36
+ return { nullValue: null };
37
+ }
38
+ else if (Array.isArray(value)) {
39
+ return {
40
+ arrayValue: {
41
+ values: value.map(item => convertToFirestoreValue(item)),
42
+ },
43
+ };
44
+ }
45
+ else if (typeof value === "object") {
46
+ const fields = Object.entries(value).reduce((acc, [key, val]) => ({
47
+ ...acc,
48
+ [key]: convertToFirestoreValue(val),
49
+ }), {});
50
+ return { mapValue: { fields } };
51
+ }
52
+ // デフォルトは文字列化
53
+ return { stringValue: String(value) };
54
+ }
55
+ /**
56
+ * Firestore形式からJSの値に変換する
57
+ * @param firestoreValue Firestore形式の値
58
+ * @returns JS形式の値
59
+ */
60
+ export function convertFromFirestoreValue(firestoreValue) {
61
+ if ("stringValue" in firestoreValue) {
62
+ return firestoreValue.stringValue;
63
+ }
64
+ else if ("integerValue" in firestoreValue) {
65
+ return Number(firestoreValue.integerValue);
66
+ }
67
+ else if ("doubleValue" in firestoreValue) {
68
+ return firestoreValue.doubleValue;
69
+ }
70
+ else if ("booleanValue" in firestoreValue) {
71
+ return firestoreValue.booleanValue;
72
+ }
73
+ else if ("nullValue" in firestoreValue) {
74
+ return null;
75
+ }
76
+ else if ("timestampValue" in firestoreValue) {
77
+ return new Date(firestoreValue.timestampValue);
78
+ }
79
+ else if ("geoPointValue" in firestoreValue) {
80
+ return new LiteralGeoPointValue(firestoreValue);
81
+ }
82
+ else if ("referenceValue" in firestoreValue) {
83
+ return new LiteralDocumentReference(firestoreValue);
84
+ }
85
+ else if ("mapValue" in firestoreValue && firestoreValue.mapValue.fields) {
86
+ return Object.entries(firestoreValue.mapValue.fields).reduce((acc, [key, val]) => ({
87
+ ...acc,
88
+ [key]: convertFromFirestoreValue(val),
89
+ }), {});
90
+ }
91
+ else if ("arrayValue" in firestoreValue &&
92
+ firestoreValue.arrayValue.values) {
93
+ return firestoreValue.arrayValue.values.map(convertFromFirestoreValue);
94
+ }
95
+ return null;
96
+ }
97
+ /**
98
+ * オブジェクトをFirestoreドキュメント形式に変換
99
+ * @param data 変換するオブジェクト
100
+ * @returns Firestoreドキュメント
101
+ */
102
+ export function convertToFirestoreDocument(data) {
103
+ return {
104
+ fields: Object.entries(data).reduce((acc, [key, value]) => ({
105
+ ...acc,
106
+ [key]: convertToFirestoreValue(value),
107
+ }), {}),
108
+ };
109
+ }
110
+ /**
111
+ * Firestoreドキュメントをオブジェクトに変換
112
+ * @param doc Firestoreレスポンス
113
+ * @returns 変換されたオブジェクト(idプロパティ付き)
114
+ */
115
+ export function convertFromFirestoreDocument(doc) {
116
+ if (!doc.fields)
117
+ return { id: getDocumentId(doc.name) };
118
+ const result = Object.entries(doc.fields).reduce((acc, [key, value]) => ({
119
+ ...acc,
120
+ [key]: convertFromFirestoreValue(value),
121
+ }), {});
122
+ return {
123
+ ...result,
124
+ id: getDocumentId(doc.name),
125
+ };
126
+ }
@@ -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;