[parse] Make Parse.Cloud.run and Parse.Cloud.define generic (#41457)

This commit is contained in:
Robby Helms
2020-01-07 15:13:42 -08:00
committed by Armando Aguirre
parent 1e79b6c502
commit 968651a22a
2 changed files with 87 additions and 5 deletions
+26 -4
View File
@@ -1011,10 +1011,14 @@ namespace Parse {
message: (response: any) => void;
}
interface FunctionRequest {
interface Params {
[key: string]: any;
}
interface FunctionRequest<T extends Params = Params> {
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<T extends (
param: { [P in keyof Parameters<T>[0]]: Parameters<T>[0][P] }
) => any>(
name: string,
func: (request: FunctionRequest<Parameters<T>[0]>) => ReturnType<T>
): 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<Object>;
function httpRequest(options: HTTPOptions): Promise<HttpResponse>;
function job(name: string, func?: (request: JobRequest) => Promise<void> | void): HttpResponse;
function run(name: string, data?: any, options?: RunOptions): Promise<any>;
function run(name: string, data?: Params, options?: RunOptions): Promise<any>;
function run<T extends () => any>(
name: string,
data?: null,
options?: RunOptions
): Promise<ReturnType<T>>;
function run<T extends (
param: { [P in keyof Parameters<T>[0]]: Parameters<T>[0][P] }
) => any>(
name: string,
data: Parameters<T>[0],
options?: RunOptions
): Promise<ReturnType<T>>;
/**
* Starts a given cloud job, which will process asynchronously.
* @param jobName The function name.
+61 -1
View File
@@ -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');
});