From d2e6498bc5f5ce60e39bbf0e94c94e31de34c646 Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Fri, 30 Apr 2021 11:19:26 +0200 Subject: [PATCH 1/4] add prettier and types for jobs (WIP) --- .prettierrc | 7 +++++++ src/types.ts | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..f0db82f --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "trailingComma": "es5", + "tabWidth": 4, + "semi": false, + "singleQuote": true, + "printWidth": 120 +} diff --git a/src/types.ts b/src/types.ts index c7a4607..636d43e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -55,6 +55,40 @@ export interface PluginMeta { global: Record } +type JobFunction = (opts?: any) => any + +type MetaInput = { + config?: Record + attachments?: Record + global?: Record + jobs?: Record any> +} + +type JobRunnerObject> = { + runNow: () => J + runIn: (duration: number, unit: string) => J +} + +type CreatePluginMetaBase = PluginMeta & { + config: Input['config'] + attachments: Input['attachments'] + global: Input['global'] + jobs: Input['jobs'] +} + +export type CreatePluginMeta = Omit, 'jobs'> & { + jobs: JobRunnerObject['jobs']> +} + +type AddMetaToJob = (opts: Parameters[0], meta: M) => ReturnType +type AddMetaToJobs> = { + [K in keyof J]: AddMetaToJob +} + +export type MetaJobsInput< + Meta extends PluginMeta & { jobs: JobRunnerObject> } +> = AddMetaToJobs> + export interface PluginConfigStructure { key?: string name?: string @@ -68,7 +102,7 @@ export interface PluginConfigStructure { export interface PluginConfigDefault extends PluginConfigStructure { type?: 'string' | 'attachment' -} +} export interface PluginConfigChoice extends PluginConfigStructure { type: 'choice' From 0848cb0171592ea88084120b46c4196ee222c61b Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Fri, 30 Apr 2021 12:58:49 +0200 Subject: [PATCH 2/4] improve type --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 636d43e..2c4135c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -61,7 +61,7 @@ type MetaInput = { config?: Record attachments?: Record global?: Record - jobs?: Record any> + jobs?: Record } type JobRunnerObject> = { From d301cb443c0bf6166193c9529b97b1c4c0d8c4ff Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Thu, 6 May 2021 11:10:08 +0200 Subject: [PATCH 3/4] update for new jobs format --- src/types.ts | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/types.ts b/src/types.ts index 2c4135c..a90cf71 100644 --- a/src/types.ts +++ b/src/types.ts @@ -53,42 +53,42 @@ export interface PluginMeta { geoip: GeoIPExtension config: Record global: Record + attachments: Record + jobs: Record JobControls> } -type JobFunction = (opts?: any) => any +type JobOptions = Record | undefined type MetaInput = { config?: Record - attachments?: Record + attachments?: Record global?: Record - jobs?: Record + jobs?: Record } -type JobRunnerObject> = { - runNow: () => J - runIn: (duration: number, unit: string) => J +type JobControls = { + runNow: () => Promise + runIn: (duration: number, unit: string) => Promise + runAt: (date: Date) => Promise } -type CreatePluginMetaBase = PluginMeta & { +type MetaJobsFromJobOptions> = { + [K in keyof J]: (opts: J[K]) => JobControls +} + +export type CreatePluginMeta = PluginMeta & { config: Input['config'] attachments: Input['attachments'] global: Input['global'] - jobs: Input['jobs'] + jobs: Input['jobs'] extends Record + ? MetaJobsFromJobOptions + : Record JobControls> } -export type CreatePluginMeta = Omit, 'jobs'> & { - jobs: JobRunnerObject['jobs']> +export type MetaJobsInput = { + [K in keyof M['jobs']]: (opts: Parameters[0], meta: M) => void | Promise } -type AddMetaToJob = (opts: Parameters[0], meta: M) => ReturnType -type AddMetaToJobs> = { - [K in keyof J]: AddMetaToJob -} - -export type MetaJobsInput< - Meta extends PluginMeta & { jobs: JobRunnerObject> } -> = AddMetaToJobs> - export interface PluginConfigStructure { key?: string name?: string From 6950a5a47ef0ef138e2a758cbc17cef882a50493 Mon Sep 17 00:00:00 2001 From: Michael Matloka Date: Fri, 7 May 2021 13:15:17 +0200 Subject: [PATCH 4/4] Only expose "creatable" PluginMeta, rename MetaJobsInput to PluginJobs --- src/types.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/types.ts b/src/types.ts index a90cf71..d84c2dc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,7 @@ import { City } from '@maxmind/geoip2-node' /** A PostHog plugin. */ -export interface Plugin { +export interface Plugin { /** Ran when the plugin is loaded by the PostHog plugin server. */ setupPlugin?: (meta: Meta) => void /** Ran when the plugin is unloaded by the PostHog plugin server. */ @@ -47,7 +47,7 @@ export interface PluginAttachment { contents: any } -export interface PluginMeta { +interface BasePluginMeta { cache: CacheExtension storage: StorageExtension geoip: GeoIPExtension @@ -76,7 +76,7 @@ type MetaJobsFromJobOptions> = { [K in keyof J]: (opts: J[K]) => JobControls } -export type CreatePluginMeta = PluginMeta & { +export interface PluginMeta extends BasePluginMeta { config: Input['config'] attachments: Input['attachments'] global: Input['global'] @@ -85,7 +85,7 @@ export type CreatePluginMeta = PluginMeta & { : Record JobControls> } -export type MetaJobsInput = { +export type PluginJobs = { [K in keyof M['jobs']]: (opts: Parameters[0], meta: M) => void | Promise }