Skip to content

Commit 94f5396

Browse files
authored
Improve typings in HTTPS and Pub/Sub functions. (#931)
Improve typings in HTTPS and Pub/Sub functions. 1. Defaulted all generic arguments for functions and types to `any` 2. Inlined handler arguments in HTTPS functions to improve code complete
1 parent 39766f0 commit 94f5396

File tree

8 files changed

+82
-35
lines changed

8 files changed

+82
-35
lines changed

spec/v2/params.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ describe('params', () => {
1717
delete process.env[TEST_PARAM];
1818
});
1919

20-
const VALUE_TESTS: {
20+
const VALUE_TESTS: Array<{
2121
method: (name: string, options: ParamOptions<any>) => Param;
22-
tests: {
22+
tests: Array<{
2323
title: string;
2424
env?: string;
2525
options?: ParamOptions;
2626
expect?: any;
2727
throws?: boolean;
28-
}[];
29-
}[] = [
28+
}>;
29+
}> = [
3030
{
3131
method: defineString,
3232
tests: [

spec/v2/providers/https.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,4 +332,21 @@ describe('onCall', () => {
332332
expect(response.body).to.be.deep.equal({ result: 42 });
333333
expect(response.headers).to.deep.equal(expectedResponseHeaders);
334334
});
335+
336+
// These tests pass if the code transpiles
337+
it('allows desirable syntax', () => {
338+
https.onCall<string, string>(
339+
(request: https.CallableRequest<string>) => `hello, ${request.data}!`
340+
);
341+
https.onCall<string>(
342+
(request: https.CallableRequest<string>) => `hello, ${request.data}!`
343+
);
344+
https.onCall<string>(
345+
(request: https.CallableRequest) => `hello, ${request.data}!`
346+
);
347+
https.onCall(
348+
(request: https.CallableRequest<string>) => `Hello, ${request.data}`
349+
);
350+
https.onCall((request: https.CallableRequest) => `Hello, ${request.data}`);
351+
});
335352
});

spec/v2/providers/pubsub.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@ describe('onMessagePublished', () => {
111111

112112
expect(json).to.deep.equal({ hello: 'world' });
113113
});
114+
115+
// These tests pass if the transpiler works
116+
it('allows desirable syntax', () => {
117+
pubsub.onMessagePublished<string>(
118+
'topic',
119+
(event: CloudEvent<pubsub.MessagePublishedData<string>>) => {}
120+
);
121+
pubsub.onMessagePublished<string>(
122+
'topic',
123+
(event: CloudEvent<pubsub.MessagePublishedData>) => {}
124+
);
125+
pubsub.onMessagePublished(
126+
'topic',
127+
(event: CloudEvent<pubsub.MessagePublishedData<string>>) => {}
128+
);
129+
pubsub.onMessagePublished(
130+
'topic',
131+
(event: CloudEvent<pubsub.MessagePublishedData>) => {}
132+
);
133+
});
114134
});

src/common/providers/https.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export interface CallableContext {
138138
/**
139139
* The request used to call a callable function.
140140
*/
141-
export interface CallableRequest<T> {
141+
export interface CallableRequest<T = any> {
142142
/**
143143
* The parameters used by a client when calling this function.
144144
*/

src/v2/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
// SOFTWARE.
2222

2323
import * as logger from '../logger';
24+
import * as params from './params';
2425
import * as https from './providers/https';
2526
import * as pubsub from './providers/pubsub';
26-
import * as params from './params';
2727

2828
export { https, pubsub, logger, params };
2929

src/v2/options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import {
2424
durationFromSeconds,
2525
serviceAccountFromShorthand,
2626
} from '../common/encoding';
27+
import { convertIfPresent, copyIfPresent } from '../common/encoding';
2728
import * as logger from '../logger';
28-
import { copyIfPresent, convertIfPresent } from '../common/encoding';
29-
import { ParamSpec } from './params/types';
30-
import { declaredParams } from './params';
3129
import { TriggerAnnotation } from './core';
30+
import { declaredParams } from './params';
31+
import { ParamSpec } from './params/types';
3232

3333
/**
3434
* List of all regions supported by Cloud Functions v2

src/v2/providers/https.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import * as options from '../options';
2828

2929
export type Request = common.Request;
3030

31-
export type CallableRequest<T> = common.CallableRequest<T>;
31+
export type CallableRequest<T = any> = common.CallableRequest<T>;
3232
export type FunctionsErrorCode = common.FunctionsErrorCode;
3333
export type HttpsError = common.HttpsError;
3434

@@ -40,33 +40,43 @@ export interface HttpsOptions extends Omit<options.GlobalOptions, 'region'> {
4040
cors?: string | boolean;
4141
}
4242

43-
export type HttpsHandler = (
44-
request: Request,
45-
response: express.Response
46-
) => void | Promise<void>;
47-
export type CallableHandler<T, Return> = (
48-
request: CallableRequest<T>
49-
) => Return;
50-
51-
export type HttpsFunction = HttpsHandler & { __trigger: unknown };
52-
export interface CallableFunction<T, Return> extends HttpsHandler {
53-
__trigger: unknown;
43+
export type HttpsFunction = ((
44+
req: Request,
45+
res: express.Response
46+
) => void | Promise<void>) & { __trigger: unknown };
47+
export interface CallableFunction<T, Return> extends HttpsFunction {
5448
run(data: CallableRequest<T>): Return;
5549
}
5650

5751
export function onRequest(
5852
opts: HttpsOptions,
59-
handler: HttpsHandler
53+
handler: (
54+
request: Request,
55+
response: express.Response
56+
) => void | Promise<void>
57+
): HttpsFunction;
58+
export function onRequest(
59+
handler: (
60+
request: Request,
61+
response: express.Response
62+
) => void | Promise<void>
6063
): HttpsFunction;
61-
export function onRequest(handler: HttpsHandler): HttpsFunction;
6264
export function onRequest(
63-
optsOrHandler: HttpsOptions | HttpsHandler,
64-
handler?: HttpsHandler
65+
optsOrHandler:
66+
| HttpsOptions
67+
| ((request: Request, response: express.Response) => void | Promise<void>),
68+
handler?: (
69+
request: Request,
70+
response: express.Response
71+
) => void | Promise<void>
6572
): HttpsFunction {
6673
let opts: HttpsOptions;
6774
if (arguments.length === 1) {
6875
opts = {};
69-
handler = optsOrHandler as HttpsHandler;
76+
handler = optsOrHandler as (
77+
request: Request,
78+
response: express.Response
79+
) => void | Promise<void>;
7080
} else {
7181
opts = optsOrHandler as HttpsOptions;
7282
}
@@ -111,19 +121,19 @@ export function onRequest(
111121

112122
export function onCall<T = any, Return = any | Promise<any>>(
113123
opts: HttpsOptions,
114-
handler: CallableHandler<T, Return>
124+
handler: (request: CallableRequest<T>) => Return
115125
): CallableFunction<T, Return>;
116126
export function onCall<T = any, Return = any | Promise<any>>(
117-
handler: CallableHandler<T, Return>
127+
handler: (request: CallableRequest<T>) => Return
118128
): CallableFunction<T, Return>;
119129
export function onCall<T = any, Return = any | Promise<any>>(
120-
optsOrHandler: HttpsOptions | CallableHandler<T, Return>,
121-
handler?: CallableHandler<T, Return>
130+
optsOrHandler: HttpsOptions | ((request: CallableRequest<T>) => Return),
131+
handler?: (request: CallableRequest<T>) => Return
122132
): CallableFunction<T, Return> {
123133
let opts: HttpsOptions;
124134
if (arguments.length == 1) {
125135
opts = {};
126-
handler = optsOrHandler as CallableHandler<T, Return>;
136+
handler = optsOrHandler as (request: CallableRequest<T>) => Return;
127137
} else {
128138
opts = optsOrHandler as HttpsOptions;
129139
}

src/v2/providers/pubsub.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class Message<T> {
8585
}
8686

8787
/** The interface published in a Pub/Sub publish subscription. */
88-
export interface MessagePublishedData<T> {
88+
export interface MessagePublishedData<T = any> {
8989
readonly message: Message<T>;
9090
readonly subscription: string;
9191
}
@@ -96,18 +96,18 @@ export interface PubSubOptions extends options.EventHandlerOptions {
9696
}
9797

9898
/** Handle a message being published to a Pub/Sub topic. */
99-
export function onMessagePublished<T = unknown>(
99+
export function onMessagePublished<T = any>(
100100
topic: string,
101101
handler: (event: CloudEvent<MessagePublishedData<T>>) => any | Promise<any>
102102
): CloudFunction<MessagePublishedData<T>>;
103103

104104
/** Handle a message being published to a Pub/Sub topic. */
105-
export function onMessagePublished<T = unknown>(
105+
export function onMessagePublished<T = any>(
106106
options: PubSubOptions,
107107
handler: (event: CloudEvent<MessagePublishedData<T>>) => any | Promise<any>
108108
): CloudFunction<MessagePublishedData<T>>;
109109

110-
export function onMessagePublished<T = unknown>(
110+
export function onMessagePublished<T = any>(
111111
topicOrOptions: string | PubSubOptions,
112112
handler: (event: CloudEvent<MessagePublishedData<T>>) => any | Promise<any>
113113
): CloudFunction<MessagePublishedData<T>> {

0 commit comments

Comments
 (0)