From 5be09d875c1b31b2e5e536dbd33fa8892a25afe2 Mon Sep 17 00:00:00 2001 From: ci7lus <7887955+ci7lus@users.noreply.github.com> Date: Thu, 6 Aug 2020 00:20:57 +0900 Subject: [PATCH] :label: Updating types --- package.json | 2 +- src/component/options.ts | 6 ++++-- src/component/pipedream.ts | 32 +++++++++++++++++++++++++------- src/component/props.ts | 18 +++++++++++++----- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 85ba5dd..f0cd1bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ironpipe", - "version": "0.0.1", + "version": "0.0.2", "author": "ci7lus <7887955+ci7lus@users.noreply.github.com>", "description": "TypeScript typed helpers for pipedream.", "repository": { diff --git a/src/component/options.ts b/src/component/options.ts index 5c7dd88..33912cd 100644 --- a/src/component/options.ts +++ b/src/component/options.ts @@ -1,6 +1,7 @@ import { MethodOptions } from "./methods" import { ExtractPropTypes, ComponentPropsOptions } from "./props" import { ObjectLiteral } from "../types" +import { ArgEventTypes } from "./pipedream" export type ComponentOptions< PropsOptions = ComponentPropsOptions, @@ -16,13 +17,14 @@ type ComponentInstance = { version: string description?: string dedupe?: string - run: (event?: any) => any | Function + run: (event?: ArgEventTypes | any) => any + hooks?: { [key: string]: Function } } type ComponentThis = ThisType< { $props: Props - $emit: (data: ObjectLiteral, data2?: ObjectLiteral) => void + $emit: (data: ObjectLiteral, metadata?: ObjectLiteral) => void } & Props & Methods & ObjectLiteral diff --git a/src/component/pipedream.ts b/src/component/pipedream.ts index 51a749d..05c057e 100644 --- a/src/component/pipedream.ts +++ b/src/component/pipedream.ts @@ -2,24 +2,42 @@ import { ObjectLiteral } from "../types" // https://github.com/PipedreamHQ/pipedream/blob/master/COMPONENT-API.md +export type ArgEventTypes = ArgEventBase | ArgEventInterval | ArgEventHttp + +export type ArgEventBase = { timestamp: number } +export type ArgEventInterval = ArgEventBase & { interval_seconds: number } +export type ArgEventHttp = { + method: "POST" | "GET" | String + path: string + query: ObjectLiteral + headers: ObjectLiteral + bodyRaw: string + body: any +} + export type PipedreamPropTypes = | "$.interface.timer" | "$.interface.http" | "$.service.db" -export type PropReturnDInterfaceTimer = {} - -export type PropDefaultDInterfaceTimer = { - intervalSeconds: number +export type PropReturnDInterfaceTimer = { + type: "$.interface.timer" } +export type PropDefaultDInterfaceTimer = + | { + intervalSeconds: number + } + | { cron: string } + export type PropReturnDInterfaceHttp = { respond(options: { - status: number - headers: ObjectLiteral - body: string | object | Buffer + status?: number + headers?: ObjectLiteral + body?: any [key: string]: any }): void + endpoint: string } export type PropReturnDServiceDB = { diff --git a/src/component/props.ts b/src/component/props.ts index f5f76f1..c8703d8 100644 --- a/src/component/props.ts +++ b/src/component/props.ts @@ -6,18 +6,20 @@ import { PropDefaultDInterfaceTimer, } from "./pipedream" -type PropTypes = "string" | "number" | PipedreamPropTypes +type PropTypes = "string" | "number" | "boolean" | PipedreamPropTypes type Prop = PropOptions | PropTypes -type PropTypesDefault = PropDefaultDInterfaceTimer | { [key: string]: any } +type PropTypesDefault = PropDefaultDInterfaceTimer type PropOptions = { type?: PropTypes | String label?: string description?: string - default?: PropTypesDefault | null - propDefinition?: T[] + default?: PropTypesDefault | { [key: string]: any } | string | null + propDefinition?: [any, string] + optional?: boolean + options?: any[] } type ConvertPropTypes = T extends null @@ -26,6 +28,8 @@ type ConvertPropTypes = T extends null ? string : T extends { type: "number" } | "number" ? number + : T extends { type: "boolean" } | "boolean" + ? boolean : T extends { type: "$.interface.timer" } | "$.interface.timer" ? PropReturnDInterfaceTimer : T extends { type: "$.interface.http" } | "$.interface.http" @@ -34,8 +38,12 @@ type ConvertPropTypes = T extends null ? PropReturnDServiceDB : any +type PropOptionalCheck = T extends { optional: true } + ? ConvertPropTypes | undefined + : ConvertPropTypes + export type ExtractPropTypes

= P extends object - ? { [K in keyof P]: ConvertPropTypes } + ? { [K in keyof P]: PropOptionalCheck } : { [K in string]: any } export type ComponentPropsOptions

> =