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