Add typing for Tencent Cloud Serverless Cloud Function (#45602)

* Add typing for Tencent Cloud Serverless Cloud Function

* Add typing for Tencent Cloud Serverless Cloud Function

* Add typing for Tencent Cloud Serverless Cloud Function
This commit is contained in:
Yaozu Lv 2020-06-23 23:23:53 +08:00 committed by GitHub
parent 3ef192e8fa
commit d9b3f178ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 349 additions and 0 deletions

View File

@ -0,0 +1,25 @@
/**
* https://intl.cloud.tencent.com/document/product/583/9210
*/
export type Handler<TEvent, TResult> = (
event: TEvent,
context: Context,
callback: Callback<TResult>,
) => void | Promise<TResult>;
export interface Context {
callbackWaitsForEmptyEventLoop: boolean;
memory_limit_in_mb: number;
time_limit_in_ms: number;
request_id: string;
environment: string | null;
environ: string | null;
function_version: string;
function_name: string;
namespace: string;
tencentcloud_region: string;
tencentcloud_appid: string;
tencentcloud_uin: string;
}
export type Callback<TResult> = (error?: Error | string | null, result?: TResult) => void;

View File

@ -0,0 +1,8 @@
// Type definitions for TencentCloud Serverless Cloud Function(SCF) 0.1
// Project: https://intl.cloud.tencent.com/product/scf
// Definitions by: Yaozu Lv <https://github.com/yaozulv>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.0
export * from './handler';
export * from './trigger';

View File

@ -0,0 +1,152 @@
import { Handler, APIGatewayEvent, TimerEvent, COSEvent, CMQTopicEvent, CKafkaEvent } from 'serverless-tencent-scf';
interface CustomEvent {
hello: 'world';
}
interface CustomResult {
success: boolean;
}
type CustomHandler = Handler<CustomEvent, CustomResult>;
let custom: CustomHandler = (event, context, callback) => {
callback(null, { success: true });
};
custom = (event, context, callback) => {
callback(new Error());
};
custom = async (event, context, callback) => {
return { success: true };
};
const apiGatewayEvent: APIGatewayEvent = {
requestContext: {
serviceId: 'service-f94sy04v',
path: '/test/{path}',
httpMethod: 'POST',
requestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
identity: {
secretId: 'abdcdxxxxxxxsdfs',
},
sourceIp: '10.0.2.14',
stage: 'release',
},
headers: {
'Accept-Language': 'en-US,en,cn',
Accept: 'text/html,application/xml,application/json',
Host: 'service-3ei3tii4-251000691.ap-guangzhou.apigateway.myqloud.com',
'User-Agent': 'User Agent String',
},
body: '{"test":"body"}',
pathParameters: {
path: 'value',
},
queryStringParameters: {
foo: 'bar',
},
headerParameters: {
Refer: '10.0.2.14',
},
stageVariables: {
stage: 'release',
},
path: '/test/value',
queryString: {
foo: 'bar',
bob: 'alice',
},
httpMethod: 'POST',
};
const timerEvent: TimerEvent = {
Type: 'Timer',
TriggerName: 'EveryDay',
Time: '2019-02-21T11:49:00Z',
Message: 'user define msg body',
};
const cosEvent: COSEvent = {
Records: [
{
cos: {
cosSchemaVersion: '1.0',
cosObject: {
url: 'http://testpic-1253970026.cos.ap-chengdu.myqcloud.com/testfile',
meta: {
'x-cos-request-id': 'NWMxOWY4MGFfMjViMjU4NjRfMTUyMVxxxxxxxxx=',
'Content-Type': '',
'x-cos-meta-mykey': 'myvalue',
},
vid: '',
key: '/1253970026/testpic/testfile',
size: 1029,
},
cosBucket: {
region: 'cd',
name: 'testpic',
appid: '1253970026',
},
cosNotificationId: 'unkown',
},
event: {
eventName: 'cos:ObjectCreated:*',
eventVersion: '1.0',
eventTime: 1545205770,
eventSource: 'qcs::cos',
requestParameters: {
requestSourceIP: '192.168.15.101',
requestHeaders: {
Authorization: 'q-sign-algorithm=sha1&q-ak=xxxxxxxxxxxxxx...',
},
},
eventQueue: 'qcs:0:lambda:cd:appid/1253970026:default.printevent.$LATEST',
reservedInfo: '',
reqid: 179398952,
},
},
],
};
const cmqTopicEvent: CMQTopicEvent = {
Records: [
{
CMQ: {
type: 'topic',
topicOwner: 1200000,
topicName: 'testtopic',
subscriptionName: 'xxxxxx',
publishTime: '1970-01-01T00:00:00.000Z',
msgId: '123345346',
requestId: '123345346',
msgBody: 'Hello from CMQ!',
msgTag: 'tag1,tag2',
},
},
],
};
const cKafkaEvent: CKafkaEvent = {
Records: [
{
Ckafka: {
topic: 'test-topic',
partition: 1,
offset: 36,
msgKey: 'None',
msgBody: 'Hello from Ckafka!',
},
},
{
Ckafka: {
topic: 'test-topic',
partition: 1,
offset: 37,
msgKey: 'None',
msgBody: 'Hello from Ckafka again!',
},
},
],
};

View File

@ -0,0 +1,130 @@
/**
* Trigger Event Message Structure
*
* https://intl.cloud.tencent.com/document/product/583/31439
*/
import { Handler } from './handler';
// API Gateway Trigger Handler
export type APIGatewayHandler<T> = Handler<APIGatewayEvent, T>;
// API Gateway Trigger Event
export interface APIGatewayEvent {
requestContext: APIGatewayRequestContext;
headers: { [name: string]: string };
body?: string | null;
pathParameters: { [name: string]: string } | null;
queryStringParameters: { [name: string]: string } | null;
headerParameters: { [name: string]: string } | null;
stageVariables: {
stage: string;
};
path: string;
queryString: { [name: string]: string } | null;
httpMethod: string;
}
// API Gateway Trigger Event RequestContext
export interface APIGatewayRequestContext {
serviceId: string;
path: string;
httpMethod: string;
requestId?: string;
identity: {
secretId?: string;
};
sourceIp: string;
stage: string;
}
// Timer Trigger Handler
export type TimerHandler<T> = Handler<TimerEvent, T>;
// Timer Trigger Event
export interface TimerEvent {
Type: string;
TriggerName: string;
Time: string;
Message: string;
}
// COS Trigger
export type COSHandler<T> = Handler<COSEvent, T>;
export interface COSEvent {
Records: COSEventRecord[];
}
export interface COSEventRecord {
cos: {
cosSchemaVersion: string;
cosObject: {
url: string;
meta: {
[name: string]: string;
};
vid: string;
key: string;
size: number;
};
cosBucket: {
region: string;
name: string;
appid: string;
};
cosNotificationId: string;
};
event: {
eventName: string;
eventVersion: string;
eventTime: number;
eventSource: string;
requestParameters: {
requestSourceIP: string;
requestHeaders: {
[name: string]: string;
};
};
eventQueue: string;
reservedInfo: string;
reqid: number;
};
}
// CMQ Topic Trigger
export type CMQTopicHandler<T> = Handler<CMQTopicEvent, T>;
export interface CMQTopicEvent {
Records: CMQTopicEventRecord[];
}
export interface CMQTopicEventRecord {
CMQ: {
type: string;
topicOwner: number;
topicName: string;
subscriptionName: string;
publishTime: string;
msgId: string;
requestId: string;
msgBody: string;
msgTag: string;
};
}
// CKafka Trigger
export type CKafkaHandler<T> = Handler<CKafkaEvent, T>;
export interface CKafkaEvent {
Records: CKafkaEventRecord[];
}
export interface CKafkaEventRecord {
Ckafka: {
topic: string;
partition: number;
offset: number;
msgKey: string;
msgBody: string;
};
}

View File

@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"jsx": "preserve"
},
"files": ["index.d.ts", "serverless-tencent-scf-tests.ts"]
}

View File

@ -0,0 +1,17 @@
{
"extends": "dtslint/dt.json",
"rules": {
"npm-naming": [
true,
{
"errors": [
[
"NeedsExportEquals",
false
]
],
"mode": "code"
}
]
}
}