Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service Account Setup #140

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions functions/models/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 31 additions & 31 deletions functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
},
"main": "lib/index.js",
"dependencies": {
"@stanfordbdhg/engagehf-models": "file:models",
"@resvg/resvg-js": "^2.6.2",
"@stanfordbdhg/engagehf-models": "file:models",
"d3": "^7.9.0",
"firebase-admin": "^12.0",
"firebase-functions": "^5.0.1",
"firebase-functions": "^5.1.1",
"jsdom": "^24.1.0",
"jspdf": "^2.5.1",
"jspdf-autotable": "^3.8.2",
Expand Down
82 changes: 9 additions & 73 deletions functions/src/functions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,17 @@ import {
} from 'firebase-functions/v2/https'
import { z } from 'zod'

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-redundant-type-constituents */

export function validatedOnCall<
Schema extends z.ZodTypeAny,
Return = any | Promise<any>,
>(
export function validatedOnCall<Schema extends z.ZodTypeAny, Return>(
name: string,
schema: Schema,
handler: (request: CallableRequest<z.output<Schema>>) => Return,
options: CallableOptions = {
invoker: 'public',
serviceAccount: `cloudfunctionsserviceaccount@${process.env.GCLOUD_PROJECT}.iam.gserviceaccount.com`,
},
): CallableFunction<
z.input<Schema>,
Return extends Promise<unknown> ? Return : Promise<Return>
> {
return onCall((request) => {
try {
logger.debug(
`onCall(${name}) from user '${request.auth?.uid}' with ${JSON.stringify(request.data)}`,
)
request.data = schema.parse(request.data) as z.output<Schema>
return handler(request)
} catch (error) {
logger.debug(
`onCall(${name}) from user '${request.auth?.uid}' failed with ${String(error)}.`,
)
if (error instanceof z.ZodError) {
throw new https.HttpsError(
'invalid-argument',
'Invalid request data',
error.errors,
)
}
throw error
}
})
}

export function validatedOnCallWithOptions<
Schema extends z.ZodTypeAny,
Return = any | Promise<any>,
>(
name: string,
schema: Schema,
options: CallableOptions,
handler: (request: CallableRequest<z.output<Schema>>) => Return,
): CallableFunction<
z.output<Schema>,
Return extends Promise<unknown> ? Return : Promise<Return>
> {
return onCall(options, (request) => {
try {
Expand Down Expand Up @@ -98,37 +61,10 @@ export function validatedOnRequest<Schema extends z.ZodTypeAny>(
data: z.output<Schema>,
response: Response,
) => void | Promise<void>,
): https.HttpsFunction {
return onRequest(async (request, response) => {
try {
logger.debug(`onRequest(${name}) with ${JSON.stringify(request.body)}`)
const data = schema.parse(request.body) as z.output<Schema>
await handler(request, data, response)
return
} catch (error) {
logger.debug(`onRequest(${name}) failed with ${String(error)}.`)
if (error instanceof z.ZodError) {
response.status(400).send({
code: 'invalid-argument',
message: 'Invalid request data',
details: error.errors,
})
return
}
throw error
}
})
}

export function validatedOnRequestWithOptions<Schema extends z.ZodTypeAny>(
name: string,
schema: Schema,
options: https.HttpsOptions,
handler: (
request: Request,
data: z.output<Schema>,
response: Response,
) => void | Promise<void>,
options: https.HttpsOptions = {
invoker: 'public',
serviceAccount: `cloudfunctionsserviceaccount@${process.env.GCLOUD_PROJECT}.iam.gserviceaccount.com`,
},
): https.HttpsFunction {
return onRequest(options, async (request, response) => {
try {
Expand Down
4 changes: 3 additions & 1 deletion functions/src/healthSummary/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ describe('generateHealthSummary', () => {
.split('\n')
.filter(
(line) =>
!line.startsWith('/CreationDate ') && !line.startsWith('/ID '),
!line.startsWith('/CreationDate ') &&
!line.startsWith('/ID ') &&
!line.startsWith('/Producer '),
)
.join('\n')
}
Expand Down
Loading