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

ref(google-cloud-serverless/v9): Update event functions to avoid any #14476

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gotta admit my concern with the function signature change wasn't the change from any to unknown for data but the change in the return value form any to void. However, TIL that TS doesn't complain about actually returning a value in a void return typed function. So technically we still should merge this only in v9 I guess. Anyway, thanks for extracting!

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 @@ -16,14 +16,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
Loading