Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Enhance skygear cloud type definition #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 80 additions & 12 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -490,6 +499,9 @@ declare module "skygear" {
}

declare module "skygear/cloud" {
import { Fields, Files } from "formidable";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that the definition is small enough that we can copy instead.
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/formidable/index.d.ts#L32

I would like to reduce the number of dependencies.

import { Url } from "url";

export interface OpParams {
action: string;
args: Array<any>;
Expand Down Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unused in this commit.

header: { [key: string]: string };
status: number;
body: string;
}
}

export type ProviderCls = Function;

Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid @types/formidable but not @types/node. Maybe we should mark @types/node as peer dependency and add it to devDependencies so that we still can reference it in our test.

},
"devDependencies": {
"typescript": "2.9.2"
},
Expand Down
19 changes: 15 additions & 4 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -132,7 +142,6 @@ const requestData = {
};
container.makeRequest("auth:signup", requestData);


skygear
.lambda("user:signup", { payload: { user: "123", password: "456" } })
.then(response => {
Expand All @@ -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);
});
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==

[email protected]:
version "2.9.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c"