Skip to content

Commit

Permalink
ref(google-cloud-serverless/v9): Update event functions to avoid any
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Nov 26, 2024
1 parent fc1d986 commit dff9a45
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
9 changes: 4 additions & 5 deletions packages/google-cloud-serverless/src/gcpfunction/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ export function wrapEventFunction(
return proxyFunction(fn, f => domainify(_wrapEventFunction(f, wrapOptions)));
}

/** */
function _wrapEventFunction<F extends EventFunction | EventFunctionWithCallback>(
fn: F,
wrapOptions: Partial<EventFunctionWrapperOptions> = {},
): (...args: Parameters<F>) => ReturnType<F> | Promise<void> {
): (...args: Parameters<F>) => void | Promise<void> {
const options: EventFunctionWrapperOptions = {
flushTimeout: 2000,
...wrapOptions,
};
return (...eventFunctionArguments: Parameters<F>): ReturnType<F> | Promise<void> => {
return (...eventFunctionArguments: Parameters<F>): void | Promise<void> => {
const [data, context, callback] = eventFunctionArguments;

return startSpanManual(
Expand All @@ -47,8 +46,8 @@ function _wrapEventFunction<F extends EventFunction | EventFunctionWithCallback>
const scope = getCurrentScope();
scope.setContext('gcp.function.context', { ...context });

const newCallback = domainify((...args: unknown[]) => {
if (args[0] !== null && args[0] !== undefined) {
const newCallback = domainify((...args): void => {
if (args[0] != null) {
captureException(args[0], scope => markEventUnhandled(scope));
}
span.end();
Expand Down
26 changes: 11 additions & 15 deletions packages/google-cloud-serverless/src/gcpfunction/general.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import type { Request, Response } from 'express';

export interface HttpFunction {
(req: Request, res: Response): any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
export type HttpFunction = (req: Request, res: Response) => void;

export interface EventFunction {
(data: Record<string, any>, context: Context): any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
export type EventFunction = (data: Record<string, unknown>, context: Context) => void;

export interface EventFunctionWithCallback {
(data: Record<string, any>, context: Context, callback: Function): any; // eslint-disable-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
}
export type EventFunctionWithCallback = (
data: Record<string, unknown>,
context: Context,
// eslint-disable-next-line @typescript-eslint/ban-types
callback: Function,
) => void;

export interface CloudEventFunction {
(cloudevent: CloudEventsContext): any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
export type CloudEventFunction = (cloudevent: CloudEventsContext) => void;

export interface CloudEventFunctionWithCallback {
(cloudevent: CloudEventsContext, callback: Function): any; // eslint-disable-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
}
// eslint-disable-next-line @typescript-eslint/ban-types
export type CloudEventFunctionWithCallback = (cloudevent: CloudEventsContext, callback: Function) => void;

export interface CloudFunctionsContext {
eventId?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/google-cloud-serverless/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export function domainify<A extends unknown[], R>(fn: (...args: A) => R): (...ar
* @returns wrapped function
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function proxyFunction<A extends any[], R, F extends (...args: A) => R>(
export function proxyFunction<F extends (...args: any[]) => unknown>(
source: F,
wrap: (source: F) => F,
overrides?: Record<PropertyKey, unknown>,
): F {
const wrapper = wrap(source);
const handler: ProxyHandler<F> = {
apply: <T>(_target: F, thisArg: T, args: A) => {
apply: <T>(_target: F, thisArg: T, args: Parameters<F>) => {
return wrapper.apply(thisArg, args);
},
};
Expand Down

0 comments on commit dff9a45

Please sign in to comment.