diff --git a/index.d.ts b/index.d.ts index a7e7493..7075cc6 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. @@ -490,6 +499,9 @@ declare module "skygear" { } declare module "skygear/cloud" { + import { Fields, Files } from "formidable"; + import { Url } from "url"; + export interface OpParams { action: string; args: Array; @@ -536,26 +548,82 @@ 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); + + setHeader(name: string, value: string): void; + getHeader(name: string): string | undefined; + removeHeader(name: string): void; + + write(chunk: string): void; + + toResultJSON(): { [key: string]: any }; + } + export 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; diff --git a/package.json b/package.json index ede04c3..083ee5e 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,13 @@ "version": "0.4.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/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); + }); 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"