From 739fc6a90df549287133c90d73dd3e437e2e634d Mon Sep 17 00:00:00 2001 From: Kenji Pa Date: Fri, 1 Mar 2019 01:34:30 +0800 Subject: [PATCH 1/3] Add skygearCloud.SkygearResponse --- index.d.ts | 25 +++++++++++++++++++++++++ test/test.ts | 19 +++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9d80960..42e3e92 100644 --- a/index.d.ts +++ b/index.d.ts @@ -556,6 +556,31 @@ declare module "skygear/cloud" { options?: handlerOptions ): void; + export class SkygearResponse { + constructor(options: SkygearResponse.Options); + + setHeader(name: string, value: string): void; + getHeader(name: string): string | undefined; + removeHeader(name: string): void; + + write(chunk: string): void; + + toResultJSON(): { [key: string]: any }; + } + namespace SkygearResponse { + interface Options { + headers?: { [key: string]: string }; + statusCode?: number; + body?: string; + } + + interface ResultJSON { + header: { [key: string]: string }; + status: number; + body: string; + } + } + export type ProviderCls = Function; export type ProviderOptions = any; diff --git a/test/test.ts b/test/test.ts index 7331c04..08e1222 100644 --- a/test/test.ts +++ b/test/test.ts @@ -42,6 +42,16 @@ skygearCloud.handler( } ); +skygearCloud.handler("skygearResponse", function() { + return new skygearCloud.SkygearResponse({ + statusCode: 404, + headers: { + "Content-Type": "application/json" + }, + body: '{"a": "b"}' + }); +}); + class ProviderCls {} skygearCloud.provides("auth", "com.facebook", ProviderCls); @@ -132,7 +142,6 @@ const requestData = { }; container.makeRequest("auth:signup", requestData); - skygear .lambda("user:signup", { payload: { user: "123", password: "456" } }) .then(response => { @@ -148,6 +157,8 @@ skygear.auth._authResolve(record).then(r => { console.log(r); }); -getSigner().sign("asset_id").then(url => { - console.log(url); -}); +getSigner() + .sign("asset_id") + .then(url => { + console.log(url); + }); From a726ee4ce2d6ad16d3318f3064437bdb9e4efc84 Mon Sep 17 00:00:00 2001 From: Kenji Pa Date: Fri, 1 Mar 2019 02:11:53 +0800 Subject: [PATCH 2/3] Enhance skygearCloud.handler type --- index.d.ts | 60 ++++++++++++++++++++++++++++++++++++++++------------ package.json | 8 ++++++- yarn.lock | 18 ++++++++++++++++ 3 files changed, 72 insertions(+), 14 deletions(-) diff --git a/index.d.ts b/index.d.ts index 42e3e92..6041e67 100644 --- a/index.d.ts +++ b/index.d.ts @@ -489,6 +489,9 @@ declare module "skygear" { } declare module "skygear/cloud" { + import { Fields, Files } from "formidable"; + import { Url } from "url"; + export interface OpParams { action: string; args: Array; @@ -535,26 +538,57 @@ declare module "skygear/cloud" { options?: EventOptions ): void; - export interface handlerOptions { + export function handler( + path: string, + func: HandlerFunc, + options?: HandlerOptions + ): void; + + export type HandlerFunc = ( + req: SkygearRequest, + options: HandlerFunc.Options + ) => any; + export namespace HandlerFunc { + interface Options { + context: { [key: string]: any }; + container: CloudCodeContainer; + } + } + + export interface HandlerOptions { method?: string[] | string; keyRequired?: boolean; userRequired?: boolean; } - export type handlerReq = any; + export class SkygearRequest { + constructor(param: SkygearRequest.Param); - export type handlerFuncOptions = any; + header: { [key: string]: string }; + method: string; + path: string; + queryString: string; + body: string; + url: Url; - export type handlerFunc = ( - req: handlerReq, - options: handlerFuncOptions - ) => any; + query: string; + json: any; - export function handler( - path: string, - func: handlerFunc, - options?: handlerOptions - ): void; + form(callback: SkygearRequest.FormCallback): void; + } + export namespace SkygearRequest { + interface Param { + header: { [key: string]: string }; + method: string; + path: string; + query_string: string; + body: string; + url: string; + } + + // it is the callback from formidable parse + type FormCallback = (err: any, fields: Fields, files: Files) => any; + } export class SkygearResponse { constructor(options: SkygearResponse.Options); @@ -567,7 +601,7 @@ declare module "skygear/cloud" { toResultJSON(): { [key: string]: any }; } - namespace SkygearResponse { + export namespace SkygearResponse { interface Options { headers?: { [key: string]: string }; statusCode?: number; diff --git a/package.json b/package.json index 4efdbca..fc2113b 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,13 @@ "version": "0.3.0", "main": "index.js", "license": "Apache-2.0", - "files": ["index.d.ts"], + "files": [ + "index.d.ts" + ], + "dependencies": { + "@types/formidable": "*", + "@types/node": "*" + }, "devDependencies": { "typescript": "2.9.2" }, diff --git a/yarn.lock b/yarn.lock index 7ec0734..e7a49db 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,6 +2,24 @@ # yarn lockfile v1 +"@types/events@*": + version "3.0.0" + resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" + integrity sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== + +"@types/formidable@*": + version "1.0.31" + resolved "https://registry.yarnpkg.com/@types/formidable/-/formidable-1.0.31.tgz#274f9dc2d0a1a9ce1feef48c24ca0859e7ec947b" + integrity sha512-dIhM5t8lRP0oWe2HF8MuPvdd1TpPTjhDMAqemcq6oIZQCBQTovhBAdTQ5L5veJB4pdQChadmHuxtB0YzqvfU3Q== + dependencies: + "@types/events" "*" + "@types/node" "*" + +"@types/node@*": + version "11.9.5" + resolved "https://registry.yarnpkg.com/@types/node/-/node-11.9.5.tgz#011eece9d3f839a806b63973e228f85967b79ed3" + integrity sha512-vVjM0SVzgaOUpflq4GYBvCpozes8OgIIS5gVXVka+OfK3hvnkC1i93U8WiY2OtNE4XUWyyy/86Kf6e0IHTQw1Q== + typescript@2.9.2: version "2.9.2" resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c" From d6bfca038cf6c052f14b96f632e6c315fbce928a Mon Sep 17 00:00:00 2001 From: Kenji Pa Date: Mon, 4 Mar 2019 14:54:52 +0800 Subject: [PATCH 3/3] Copy Blob interface from dom type definition --- index.d.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.d.ts b/index.d.ts index 6041e67..b21318a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -62,6 +62,15 @@ declare module "skygear" { constructor(name: string); } + // NOTE(limouren): Node doesn't have Blob. Copy the type definition here to + // make tsc happy. + // https://github.com/Microsoft/TypeScript/blob/8794ebdff5c00a92bb197a5afba02d573681538e/src/lib/dom.generated.d.ts#L2233 + interface Blob { + readonly size: number; + readonly type: string; + slice(start?: number, end?: number, contentType?: string): Blob; + } + // NOTE(louis): I do not want to copy this but // in react native environment we cannot use --lib DOM // so File is undefined.