From 95b0e719b85fafc77943ded279d8f4a1ec790d03 Mon Sep 17 00:00:00 2001 From: Christopher Eck <13987499+chris-codaio@users.noreply.github.com> Date: Mon, 15 Mar 2021 11:50:25 -0700 Subject: [PATCH] Add type for valid fetch methods (#857) * Add type for valid fetch methods * Address feedback --- api_types.ts | 10 +++++++++- dist/api_types.d.ts | 4 +++- dist/api_types.js | 8 ++++++++ dist/handler_templates.d.ts | 3 ++- dist/index.d.ts | 1 + handler_templates.ts | 3 ++- index.ts | 1 + 7 files changed, 26 insertions(+), 4 deletions(-) diff --git a/api_types.ts b/api_types.ts index cab4d954ee..937ac786e4 100644 --- a/api_types.ts +++ b/api_types.ts @@ -135,10 +135,18 @@ export interface Network { } // Fetcher APIs +const ValidFetchMethods = [ + 'GET', + 'PATCH', + 'POST', + 'PUT', + 'DELETE', +] as const; +export type FetchMethodType = typeof ValidFetchMethods[number]; // Copied from https://developer.mozilla.org/en-US/docs/Web/API/Request export interface FetchRequest { - method: 'GET' | 'PATCH' | 'POST' | 'PUT' | 'DELETE'; + method: FetchMethodType; url: string; body?: string; form?: {[key: string]: string}; diff --git a/dist/api_types.d.ts b/dist/api_types.d.ts index c6f8a5b037..18384c8381 100644 --- a/dist/api_types.d.ts +++ b/dist/api_types.d.ts @@ -77,8 +77,10 @@ export interface Network { readonly hasSideEffect?: boolean; readonly requiresConnection?: boolean; } +declare const ValidFetchMethods: readonly ["GET", "PATCH", "POST", "PUT", "DELETE"]; +export declare type FetchMethodType = typeof ValidFetchMethods[number]; export interface FetchRequest { - method: 'GET' | 'PATCH' | 'POST' | 'PUT' | 'DELETE'; + method: FetchMethodType; url: string; body?: string; form?: { diff --git a/dist/api_types.js b/dist/api_types.js index f252823023..c3c41e9538 100644 --- a/dist/api_types.js +++ b/dist/api_types.js @@ -39,6 +39,14 @@ exports.imageArray = { type: 'array', items: Type.image, }; +// Fetcher APIs +const ValidFetchMethods = [ + 'GET', + 'PATCH', + 'POST', + 'PUT', + 'DELETE', +]; // A mapping exists in coda that allows these to show up in the UI. // If adding new values here, add them to that mapping and vice versa. var PrecannedDateRange; diff --git a/dist/handler_templates.d.ts b/dist/handler_templates.d.ts index 9e02ca04d2..5dea832980 100644 --- a/dist/handler_templates.d.ts +++ b/dist/handler_templates.d.ts @@ -1,3 +1,4 @@ +import type { FetchMethodType } from './api_types'; import type { FetchRequest } from './api_types'; import type { FetchResponse } from './api_types'; import type { PackFormulaValue } from './api_types'; @@ -7,7 +8,7 @@ import type { SchemaType } from './schema'; declare type ParamMapper = (val: T) => T; export interface RequestHandlerTemplate { url: string; - method: 'GET' | 'PATCH' | 'POST' | 'PUT' | 'DELETE'; + method: FetchMethodType; headers?: { [header: string]: string; }; diff --git a/dist/index.d.ts b/dist/index.d.ts index 4e17b20af9..4be8ebb3fe 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -36,6 +36,7 @@ export type { DynamicSyncTableDef } from './api'; export type { EmptyFormulaDef } from './api'; export type { ExecutionContext } from './api_types'; export type { Fetcher } from './api_types'; +export type { FetchMethodType } from './api_types'; export type { FetchRequest } from './api_types'; export type { FetchResponse } from './api_types'; export type { GenericDynamicSyncTable } from './api'; diff --git a/handler_templates.ts b/handler_templates.ts index e0c44c274b..90fc573758 100644 --- a/handler_templates.ts +++ b/handler_templates.ts @@ -1,3 +1,4 @@ +import type {FetchMethodType} from './api_types'; import type {FetchRequest} from './api_types'; import type {FetchResponse} from './api_types'; import type {ObjectSchemaProperty} from './schema'; @@ -17,7 +18,7 @@ type ParamMapper = (val: T) => T; export interface RequestHandlerTemplate { url: string; - method: 'GET' | 'PATCH' | 'POST' | 'PUT' | 'DELETE'; + method: FetchMethodType; headers?: {[header: string]: string}; nameMapping?: {[functionParamName: string]: string}; transforms?: {[name: string]: ParamMapper}; diff --git a/index.ts b/index.ts index 01da0383f8..38e423422a 100644 --- a/index.ts +++ b/index.ts @@ -43,6 +43,7 @@ export type {DynamicSyncTableDef} from './api'; export type {EmptyFormulaDef} from './api'; export type {ExecutionContext} from './api_types'; export type {Fetcher} from './api_types'; +export type {FetchMethodType} from './api_types'; export type {FetchRequest} from './api_types'; export type {FetchResponse} from './api_types'; export type {GenericDynamicSyncTable} from './api';