From dd115f4c25a84dd9fb033aaa841586eeba037f40 Mon Sep 17 00:00:00 2001 From: kiranpandit Date: Thu, 12 Aug 2021 16:07:19 -0700 Subject: [PATCH] Add update database to client (#170) * Add update database to client * Fix typo * Change response to Database * Use the same type for both select and multi select options --- src/Client.ts | 18 ++++++++++++++++++ src/api-endpoints.ts | 34 +++++++++++++++++++++++++++++++++- src/api-types.ts | 24 ++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/Client.ts b/src/Client.ts index 201142c6..5889e5d4 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -55,6 +55,9 @@ import { BlocksUpdateParameters, BlocksUpdateResponse, blocksUpdate, + databasesUpdate, + DatabasesUpdateParameters, + DatabasesUpdateResponse, } from "./api-endpoints" import nodeFetch from "node-fetch" import { @@ -320,6 +323,21 @@ export default class Client { auth: args?.auth, }) }, + + /** + * Update a database + */ + update: ( + args: WithAuth + ): Promise => { + return this.request({ + path: databasesUpdate.path(args), + method: databasesUpdate.method, + query: pick(args, databasesUpdate.queryParams), + body: pick(args, databasesUpdate.bodyParams), + auth: args?.auth, + }) + }, } public readonly pages = { diff --git a/src/api-endpoints.ts b/src/api-endpoints.ts index a2ecb16c..b5b55b5c 100644 --- a/src/api-endpoints.ts +++ b/src/api-endpoints.ts @@ -27,6 +27,7 @@ import { PropertySchema, RichTextInput, UpdateBlock, + UpdatePropertySchema, } from "./api-types" // TODO: type assertions to verify that each interface is synchronized to the list of keys in the runtime value below. @@ -267,7 +268,7 @@ export interface DatabasesCreateParameters extends DatabasesCreatePathParameters, DatabasesCreateQueryParameters, DatabasesCreateBodyParameters {} -export interface DatabasesCreateResponse extends BlockBase {} +export interface DatabasesCreateResponse extends Database {} export const databasesCreate = { method: "post", @@ -277,6 +278,37 @@ export const databasesCreate = { path: () => `databases`, } as const +/* + * databases.update() + */ + +interface DatabasesUpdatePathParameters { + database_id: string +} +interface DatabasesUpdateQueryParameters {} + +export type UpdatePropertySchemaMap = { + [propertyName: string]: UpdatePropertySchema +} +interface DatabasesUpdateBodyParameters { + properties?: UpdatePropertySchemaMap + title?: RichTextInput[] +} + +export interface DatabasesUpdateParameters + extends DatabasesUpdatePathParameters, + DatabasesUpdateQueryParameters, + DatabasesUpdateBodyParameters {} +export interface DatabasesUpdateResponse extends Database {} + +export const databasesUpdate = { + method: "patch", + pathParams: ["database_id"], + queryParams: [], + bodyParams: ["properties", "title"], + path: (d: DatabasesUpdatePathParameters) => `databases/${d.database_id}`, +} as const + /* * pages.retrieve() */ diff --git a/src/api-types.ts b/src/api-types.ts index ecf27cf3..f6346d53 100644 --- a/src/api-types.ts +++ b/src/api-types.ts @@ -1040,3 +1040,27 @@ export interface LastEditedTimePropertySchema { export interface LastEditedByPropertySchema { last_edited_by: Record } + +/* + * Update database property schema (input) + */ +export interface RenamePropertySchema { + name: string +} + +type UpdateSelectOptionSchema = SelectOptionSchema | SelectOption + +export interface UpdateSelectPropertySchema { + select: { options?: UpdateSelectOptionSchema[] } +} + +export interface UpdateMultiSelectPropertySchema { + multi_select: { options?: UpdateSelectOptionSchema[] } +} + +export type UpdatePropertySchema = + | PropertySchema + | UpdateSelectPropertySchema + | UpdateMultiSelectPropertySchema + | RenamePropertySchema + | null