From 968651a22aa9422b8393cbf69a9458ee937cf06e Mon Sep 17 00:00:00 2001 From: Robby Helms Date: Tue, 7 Jan 2020 18:13:42 -0500 Subject: [PATCH] [parse] Make Parse.Cloud.run and Parse.Cloud.define generic (#41457) --- types/parse/index.d.ts | 30 +++++++++++++++--- types/parse/parse-tests.ts | 62 +++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 5 deletions(-) diff --git a/types/parse/index.d.ts b/types/parse/index.d.ts index 1b4f2bc8c5..5536ac499c 100644 --- a/types/parse/index.d.ts +++ b/types/parse/index.d.ts @@ -1011,10 +1011,14 @@ namespace Parse { message: (response: any) => void; } - interface FunctionRequest { + interface Params { + [key: string]: any; + } + + interface FunctionRequest { installationId?: string; master?: boolean; - params?: any; + params: T; user?: User; } @@ -1075,7 +1079,13 @@ namespace Parse { ): void; function afterFind(arg1: any, func?: (request: AfterFindRequest) => any): void; function beforeLogin(func?: (request: TriggerRequest) => any): void; - function define(name: string, func?: (request: FunctionRequest) => any): void; + function define(name: string, func: (request: FunctionRequest) => any): void; + function define[0]]: Parameters[0][P] } + ) => any>( + name: string, + func: (request: FunctionRequest[0]>) => ReturnType + ): void; /** * Gets data for the current set of cloud jobs. * @returns A promise that will be resolved with the result of the function. @@ -1089,7 +1099,19 @@ namespace Parse { function getJobStatus(jobStatusId: string): Promise; function httpRequest(options: HTTPOptions): Promise; function job(name: string, func?: (request: JobRequest) => Promise | void): HttpResponse; - function run(name: string, data?: any, options?: RunOptions): Promise; + function run(name: string, data?: Params, options?: RunOptions): Promise; + function run any>( + name: string, + data?: null, + options?: RunOptions + ): Promise>; + function run[0]]: Parameters[0][P] } + ) => any>( + name: string, + data: Parameters[0], + options?: RunOptions + ): Promise>; /** * Starts a given cloud job, which will process asynchronously. * @param jobName The function name. diff --git a/types/parse/parse-tests.ts b/types/parse/parse-tests.ts index 569486cef6..23446c2be9 100644 --- a/types/parse/parse-tests.ts +++ b/types/parse/parse-tests.ts @@ -430,7 +430,7 @@ function test_facebook_util() { }); } -function test_cloud_functions() { +async function test_cloud_functions() { Parse.Cloud.run( 'hello', {}, @@ -442,6 +442,39 @@ function test_cloud_functions() { }, ); + // $ExpectType any + await Parse.Cloud.run('SomeFunction'); + + // $ExpectType any + await Parse.Cloud.run('SomeFunction', { something: 'whatever' }); + + // $ExpectType any + await Parse.Cloud.run('SomeFunction', null, { useMasterKey: true }); + + // ExpectType boolean + await Parse.Cloud.run<() => boolean>('SomeFunction'); + + // $ExpectType boolean + await Parse.Cloud.run<() => boolean>('SomeFunction', null); + + // $ExpectType boolean + await Parse.Cloud.run<() => boolean>('SomeFunction', null, { useMasterKey: true }); + + // $ExpectType number + await Parse.Cloud.run<(params: { paramA: string }) => number>('SomeFunction', { paramA: 'hello' }); + + // $ExpectError + await Parse.Cloud.run<(params: { paramA: string }) => number>('SomeFunction'); + + // $ExpectError + await Parse.Cloud.run<(params: { paramA: string }) => number>('SomeFunction', { paramZ: 'hello' }); + + // $ExpectError + await Parse.Cloud.run<(params: { paramA: string}) => number>('SomeFunction', null, { useMasterKey: true }); + + // $ExpectError + await Parse.Cloud.run<(params: string) => any>('SomeFunction', 'hello'); + Parse.Cloud.afterDelete('MyCustomClass', (request: Parse.Cloud.AfterDeleteRequest) => { // result }); @@ -529,6 +562,33 @@ function test_cloud_functions() { return 'Some result'; }); + Parse.Cloud.define('AFunc', (request) => { + // $ExpectType Params + request.params; + + // $ExpectType any + request.params.anything; + }); + + Parse.Cloud.define<(params: { something: string }) => number>('AFunc', (request) => { + // $ExpectType { something: string; } + request.params; + + // $ExpectError + request.params.somethingElse; + + return 123; + }); + + // $ExpectError + Parse.Cloud.define('AFunc'); + + // $ExpectError + Parse.Cloud.define<() => string>('AFunc', () => 123); + + // $ExpectError + Parse.Cloud.define<(params: string) => number>('AFunc', () => 123); + Parse.Cloud.job('AJob', (request: Parse.Cloud.JobRequest) => { request.message('Message to associate with this job run'); });