refactor: create function to read admin SDK JSON
This commit is contained in:
27
src/libs/gcloud/getAdminSdkCredentials.ts
Normal file
27
src/libs/gcloud/getAdminSdkCredentials.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import mapKeys from "lodash.mapkeys";
|
||||
|
||||
import type { Env } from "~/types/env";
|
||||
|
||||
import { Case, changeStringCase } from "../changeStringCase";
|
||||
import { readEnvVariable } from "../readEnvVariable";
|
||||
|
||||
export function getAdminSdkCredentials(env: Env) {
|
||||
return mapKeys(
|
||||
readEnvVariable<AdminSdkCredentials>(env, "ADMIN_SDK_JSON"),
|
||||
(_, key) => changeStringCase(key, Case.snake_case, Case.camelCase),
|
||||
) as unknown as AdminSdkCredentials;
|
||||
}
|
||||
|
||||
export interface AdminSdkCredentials {
|
||||
type: string;
|
||||
projectId: string;
|
||||
privateKeyId: string;
|
||||
privateKey: string;
|
||||
clientEmail: string;
|
||||
clientID: string;
|
||||
authURI: string;
|
||||
tokenURI: string;
|
||||
authProviderX509CertUrl: string;
|
||||
clientX509CertUrl: string;
|
||||
universeDomain: string;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { SignJWT, importPKCS8 } from "jose";
|
||||
import { DateTime } from "luxon";
|
||||
|
||||
import { lazy } from "../lazy";
|
||||
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
|
||||
|
||||
export async function getGoogleAuthToken(adminSdkJson: AdminSdkCredentials) {
|
||||
const { privateKey, clientEmail } = adminSdkJson;
|
||||
@@ -11,7 +12,10 @@ export async function getGoogleAuthToken(adminSdkJson: AdminSdkCredentials) {
|
||||
{
|
||||
key: privateKey,
|
||||
email: clientEmail,
|
||||
scope: ["https://www.googleapis.com/auth/firebase.messaging"],
|
||||
scope: [
|
||||
"https://www.googleapis.com/auth/firebase.messaging",
|
||||
"https://www.googleapis.com/auth/cloud-tasks",
|
||||
],
|
||||
},
|
||||
adminSdkJson,
|
||||
);
|
||||
@@ -155,17 +159,3 @@ interface TokenData {
|
||||
token: GoogleTokenData;
|
||||
expiresAt: DateTime;
|
||||
}
|
||||
|
||||
export interface AdminSdkCredentials {
|
||||
type: string;
|
||||
projectId: string;
|
||||
privateKeyId: string;
|
||||
privateKey: string;
|
||||
clientEmail: string;
|
||||
clientID: string;
|
||||
authURI: string;
|
||||
tokenURI: string;
|
||||
authProviderX509CertUrl: string;
|
||||
clientX509CertUrl: string;
|
||||
universeDomain: string;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import {
|
||||
type AdminSdkCredentials,
|
||||
getGoogleAuthToken,
|
||||
} from "./getGoogleAuthToken";
|
||||
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
|
||||
import { getGoogleAuthToken } from "./getGoogleAuthToken";
|
||||
|
||||
export async function sendFcmMessage(
|
||||
adminSdkJson: AdminSdkCredentials,
|
||||
@@ -20,7 +18,7 @@ export async function sendFcmMessage(
|
||||
Authorization: `Bearer ${await getGoogleAuthToken(adminSdkJson)}`,
|
||||
},
|
||||
},
|
||||
).then((res) => res.json<SendFcmMessageResponse>());
|
||||
).then((res) => res.json() as Promise<SendFcmMessageResponse>);
|
||||
}
|
||||
|
||||
type SendFcmMessageResponse =
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, expect, it } from "bun:test";
|
||||
|
||||
import { server } from "~/mocks";
|
||||
|
||||
import type { AdminSdkCredentials } from "./getGoogleAuthToken";
|
||||
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
|
||||
import { verifyFcmToken } from "./verifyFcmToken";
|
||||
|
||||
server.listen();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { AdminSdkCredentials } from "./getGoogleAuthToken";
|
||||
import type { AdminSdkCredentials } from "./getAdminSdkCredentials";
|
||||
import { sendFcmMessage } from "./sendFcmMessage";
|
||||
|
||||
export async function verifyFcmToken(
|
||||
|
||||
Reference in New Issue
Block a user