From 7b51c18b9161c96ab2041c39b83c6d68243cf9d8 Mon Sep 17 00:00:00 2001 From: litencatt Date: Mon, 1 Jan 2024 23:39:52 +0900 Subject: [PATCH] Apply prettier to .ts --- src/commands/block/append.ts | 23 +- src/commands/block/delete.ts | 21 +- src/commands/block/retrieve.ts | 23 +- src/commands/block/retrieve/children.ts | 21 +- src/commands/block/update.ts | 21 +- src/commands/db/create.ts | 41 ++-- src/commands/db/query.ts | 193 ++++++++++------- src/commands/db/retrieve.ts | 36 ++-- src/commands/db/update.ts | 57 ++--- src/commands/page/create.ts | 20 +- src/commands/page/retrieve.ts | 20 +- src/commands/page/retrieve/property_item.ts | 4 +- src/commands/page/update.ts | 22 +- src/commands/search.ts | 31 +-- src/commands/user/list.ts | 14 +- src/commands/user/retrieve.ts | 14 +- src/commands/user/retrieve/bot.ts | 14 +- src/helper.ts | 204 ++++++++---------- src/notion.ts | 43 ++-- test/commands/block/append.test.ts | 64 +++--- test/commands/block/delete.test.ts | 40 ++-- test/commands/block/retrieve.test.ts | 34 +-- test/commands/block/retrieve/children.test.ts | 44 ++-- test/commands/block/update.test.ts | 40 ++-- test/commands/db/create.test.ts | 62 +++--- test/commands/db/query.test.ts | 71 +++--- test/commands/db/retrieve.test.ts | 61 +++--- test/commands/db/update.test.ts | 64 +++--- test/commands/page/create.test.ts | 115 +++++----- test/commands/page/retrieve.test.ts | 115 +++++----- .../page/retrieve/property_item.test.ts | 27 +-- test/commands/page/update.test.ts | 147 ++++++------- test/commands/search.test.ts | 85 ++++---- test/commands/user/list.test.ts | 39 ++-- test/commands/user/retrieve.test.ts | 70 +++--- test/commands/user/retrieve/bot.test.ts | 64 +++--- test/helper.test.ts | 37 ++-- 37 files changed, 948 insertions(+), 1053 deletions(-) diff --git a/src/commands/block/append.ts b/src/commands/block/append.ts index 8ea344b..6fd8849 100644 --- a/src/commands/block/append.ts +++ b/src/commands/block/append.ts @@ -1,13 +1,10 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../notion' import { AppendBlockChildrenParameters, - BlockObjectResponse + BlockObjectResponse, } from '@notionhq/client/build/src/api-endpoints' -import { - getBlockPlainText, - outputRawJson -} from '../../helper' +import { getBlockPlainText, outputRawJson } from '../../helper' export default class BlockAppend extends Command { static description = 'Append block children' @@ -30,9 +27,9 @@ export default class BlockAppend extends Command { ] static args = { - block_id: Args.string({required: true}), - children: Args.string({required: true}), - after: Args.string({required: false}), + block_id: Args.string({ required: true }), + children: Args.string({ required: true }), + after: Args.string({ required: false }), } static flags = { @@ -45,7 +42,7 @@ export default class BlockAppend extends Command { // TODO: Add support children params building prompt public async run(): Promise { - const {args, flags} = await this.parse(BlockAppend) + const { args, flags } = await this.parse(BlockAppend) const params: AppendBlockChildrenParameters = { block_id: args.block_id, children: JSON.parse(args.children), @@ -67,12 +64,12 @@ export default class BlockAppend extends Command { content: { get: (row: BlockObjectResponse) => { return getBlockPlainText(row) - } - } + }, + }, } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table(res.results, columns, options) } diff --git a/src/commands/block/delete.ts b/src/commands/block/delete.ts index a42768a..0a45e6d 100644 --- a/src/commands/block/delete.ts +++ b/src/commands/block/delete.ts @@ -1,12 +1,7 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../notion' -import { - BlockObjectResponse -} from '@notionhq/client/build/src/api-endpoints' -import { - getBlockPlainText, - outputRawJson -} from '../../helper' +import { BlockObjectResponse } from '@notionhq/client/build/src/api-endpoints' +import { getBlockPlainText, outputRawJson } from '../../helper' export default class BlockDelete extends Command { static description = 'Delete a block' @@ -25,7 +20,7 @@ export default class BlockDelete extends Command { ] static args = { - block_id: Args.string({required: true}), + block_id: Args.string({ required: true }), } static flags = { @@ -37,7 +32,7 @@ export default class BlockDelete extends Command { } public async run(): Promise { - const {args, flags} = await this.parse(BlockDelete) + const { args, flags } = await this.parse(BlockDelete) const res = await notion.deleteBlock(args.block_id) if (flags.raw) { @@ -53,12 +48,12 @@ export default class BlockDelete extends Command { content: { get: (row: BlockObjectResponse) => { return getBlockPlainText(row) - } - } + }, + }, } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/block/retrieve.ts b/src/commands/block/retrieve.ts index 3712187..a2fb9cf 100644 --- a/src/commands/block/retrieve.ts +++ b/src/commands/block/retrieve.ts @@ -1,12 +1,7 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../notion' -import { - BlockObjectResponse -} from '@notionhq/client/build/src/api-endpoints' -import { - getBlockPlainText, - outputRawJson, -} from '../../helper' +import { BlockObjectResponse } from '@notionhq/client/build/src/api-endpoints' +import { getBlockPlainText, outputRawJson } from '../../helper' export default class BlockRetrieve extends Command { static description = 'Retrieve a block' @@ -21,11 +16,11 @@ export default class BlockRetrieve extends Command { { description: 'Retrieve a block and output raw json', command: `$ notion-cli block retrieve BLOCK_ID -r`, - } + }, ] static args = { - block_id: Args.string({required: true}), + block_id: Args.string({ required: true }), } static flags = { @@ -37,7 +32,7 @@ export default class BlockRetrieve extends Command { } public async run(): Promise { - const {args, flags} = await this.parse(BlockRetrieve) + const { args, flags } = await this.parse(BlockRetrieve) const res = await notion.retrieveBlock(args.block_id) if (flags.raw) { @@ -53,12 +48,12 @@ export default class BlockRetrieve extends Command { content: { get: (row: BlockObjectResponse) => { return getBlockPlainText(row) - } - } + }, + }, } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/block/retrieve/children.ts b/src/commands/block/retrieve/children.ts index 8b34bc3..2e0bcd1 100644 --- a/src/commands/block/retrieve/children.ts +++ b/src/commands/block/retrieve/children.ts @@ -1,12 +1,7 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../../notion' -import { - BlockObjectResponse -} from '@notionhq/client/build/src/api-endpoints' -import { - getBlockPlainText, - outputRawJson, -} from '../../../helper' +import { BlockObjectResponse } from '@notionhq/client/build/src/api-endpoints' +import { getBlockPlainText, outputRawJson } from '../../../helper' export default class BlockRetrieveChildren extends Command { static description = 'Retrieve block children' @@ -21,13 +16,13 @@ export default class BlockRetrieveChildren extends Command { { description: 'Retrieve block children and output raw json', command: `$ notion-cli block retrieve:children BLOCK_ID -r`, - } + }, ] static args = { block_id: Args.string({ description: 'block_id or page_id', - required: true + required: true, }), } @@ -40,7 +35,7 @@ export default class BlockRetrieveChildren extends Command { } public async run(): Promise { - const {args, flags} = await this.parse(BlockRetrieveChildren) + const { args, flags } = await this.parse(BlockRetrieveChildren) // TODO: Add support start_cursor, page_size const res = await notion.retrieveBlockChildren(args.block_id) @@ -56,12 +51,12 @@ export default class BlockRetrieveChildren extends Command { content: { get: (row: BlockObjectResponse) => { return getBlockPlainText(row) - } + }, }, } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table(res.results, columns, options) } diff --git a/src/commands/block/update.ts b/src/commands/block/update.ts index 10dbbb5..bb25ed1 100644 --- a/src/commands/block/update.ts +++ b/src/commands/block/update.ts @@ -1,13 +1,10 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../notion' import { UpdateBlockParameters, BlockObjectResponse, } from '@notionhq/client/build/src/api-endpoints' -import { - outputRawJson, - getBlockPlainText, -} from '../../helper' +import { outputRawJson, getBlockPlainText } from '../../helper' export default class BlockUpdate extends Command { static description = 'Update a block' @@ -22,15 +19,15 @@ export default class BlockUpdate extends Command { { description: 'Update a block and output raw json', command: `$ notion-cli block update BLOCK_ID -r`, - } + }, ] static args = { - block_id: Args.string({descriptin: 'block_id', required: true}), + block_id: Args.string({ descriptin: 'block_id', required: true }), } static flags = { - archived: Flags.boolean({ char: 'a'}), + archived: Flags.boolean({ char: 'a' }), raw: Flags.boolean({ char: 'r', description: 'output raw json', @@ -40,7 +37,7 @@ export default class BlockUpdate extends Command { // TODO: Add support for updating block type objects public async run(): Promise { - const {args, flags} = await this.parse(BlockUpdate) + const { args, flags } = await this.parse(BlockUpdate) // Support only archived for now const params: UpdateBlockParameters = { block_id: args.block_id, @@ -60,12 +57,12 @@ export default class BlockUpdate extends Command { content: { get: (row: BlockObjectResponse) => { return getBlockPlainText(row) - } - } + }, + }, } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/db/create.ts b/src/commands/db/create.ts index 2ba0548..75ee9eb 100644 --- a/src/commands/db/create.ts +++ b/src/commands/db/create.ts @@ -1,16 +1,12 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import { CreateDatabaseParameters, DatabaseObjectResponse, } from '@notionhq/client/build/src/api-endpoints' import * as notion from '../../notion' -import { - onCancel, - outputRawJson, - getDbTitle, -} from '../../helper' +import { onCancel, outputRawJson, getDbTitle } from '../../helper' -const prompts = require('prompts') +const prompts = require('prompts') export default class DbCreate extends Command { static description = 'Create a database' @@ -37,17 +33,17 @@ export default class DbCreate extends Command { { description: 'Create a database with a specific page_id and output raw json with title', command: `$ notion-cli db create PAGE_ID -t 'My Database' -r`, - } + }, ] static args = { - page_id: Args.string({required: true}), + page_id: Args.string({ required: true }), } static flags = { title: Flags.string({ char: 't', - description: 'new database title' + description: 'new database title', }), raw: Flags.boolean({ char: 'r', @@ -62,11 +58,16 @@ export default class DbCreate extends Command { let dbTitle = flags.title if (dbTitle == undefined) { - const dbPropPromptResult = await prompts([{ - type: 'text', - name: 'title', - message: 'Please input database title', - }], { onCancel }) + const dbPropPromptResult = await prompts( + [ + { + type: 'text', + name: 'title', + message: 'Please input database title', + }, + ], + { onCancel } + ) console.log(dbPropPromptResult) dbTitle = dbPropPromptResult.title @@ -83,13 +84,13 @@ export default class DbCreate extends Command { type: 'text', text: { content: dbTitle, - } - } + }, + }, ], properties: { Name: { - title: {} - } + title: {}, + }, }, } @@ -111,7 +112,7 @@ export default class DbCreate extends Command { } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/db/query.ts b/src/commands/db/query.ts index f41a2ae..85954c4 100644 --- a/src/commands/db/query.ts +++ b/src/commands/db/query.ts @@ -1,4 +1,4 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../notion' import { PageObjectResponse, @@ -8,18 +8,18 @@ import * as fs from 'fs' import * as path from 'path' import * as dayjs from 'dayjs' import { - buildFilterPagePrompt, - buildDatabaseQueryFilter, - getDbChoices, - getPromptChoices, - getFilterFields, - onCancel, - outputRawJson, - getDbTitle, - getPageTitle, - } from '../../helper' + buildFilterPagePrompt, + buildDatabaseQueryFilter, + getDbChoices, + getPromptChoices, + getFilterFields, + onCancel, + outputRawJson, + getDbTitle, + getPageTitle, +} from '../../helper' -const prompts = require('prompts') +const prompts = require('prompts') export default class DbQuery extends Command { static description = 'Query a database' @@ -56,11 +56,11 @@ export default class DbQuery extends Command { static flags = { rawFilter: Flags.string({ char: 'a', - description: 'JSON stringified filter string' + description: 'JSON stringified filter string', }), fileFilter: Flags.string({ char: 'f', - description: 'JSON filter file path' + description: 'JSON filter file path', }), raw: Flags.boolean({ char: 'r', @@ -75,12 +75,17 @@ export default class DbQuery extends Command { let databaseId = args.database_id if (databaseId == undefined) { const dbChoices = await getDbChoices() - const promptSelectedDbResult = await prompts([{ - message: 'Select a database to query', - type: 'autocomplete', - name: 'database_id', - choices: dbChoices - }], { onCancel }) + const promptSelectedDbResult = await prompts( + [ + { + message: 'Select a database to query', + type: 'autocomplete', + name: 'database_id', + choices: dbChoices, + }, + ], + { onCancel } + ) if (process.env.DEBUG) { this.log(promptSelectedDbResult) } @@ -99,73 +104,89 @@ export default class DbQuery extends Command { } else { let CombineOperator = undefined - const promptAddFilterResult = await prompts([{ - message: 'Add filter?', - type: 'confirm', - name: 'value', - initial: true - }], { onCancel }) + const promptAddFilterResult = await prompts( + [ + { + message: 'Add filter?', + type: 'confirm', + name: 'value', + initial: true, + }, + ], + { onCancel } + ) const selectedDb = await notion.retrieveDb(databaseId) const dbPropsChoices = await getPromptChoices(selectedDb) if (process.env.DEBUG) { - console.dir(dbPropsChoices, {depth: null}) + console.dir(dbPropsChoices, { depth: null }) } while (promptAddFilterResult.value) { // Choice the operator first time and keep using it. if (filter != undefined && CombineOperator == undefined) { - const promptAndOrPropResult = await prompts([{ - message: 'Select and/or', - type: 'autocomplete', - name: 'operator', - choices: [ - { title: 'and'}, - { title: 'or'}, - ] - }], { onCancel }) + const promptAndOrPropResult = await prompts( + [ + { + message: 'Select and/or', + type: 'autocomplete', + name: 'operator', + choices: [{ title: 'and' }, { title: 'or' }], + }, + ], + { onCancel } + ) // rebuild filter object with choose operator const tmp = filter CombineOperator = promptAndOrPropResult.operator - filter = {[CombineOperator]: [tmp]} + filter = { [CombineOperator]: [tmp] } if (process.env.DEBUG) { - console.dir(filter, {depth: null}) + console.dir(filter, { depth: null }) } } - const promptSelectFilterPropResult = await prompts([{ - message: 'Select a property for filter by', - type: 'autocomplete', - name: 'property', - choices: dbPropsChoices - }], { onCancel }) + const promptSelectFilterPropResult = await prompts( + [ + { + message: 'Select a property for filter by', + type: 'autocomplete', + name: 'property', + choices: dbPropsChoices, + }, + ], + { onCancel } + ) // 選ばれたプロパティのタイプに応じて次のプロンプト情報を作成する. // 同一DBでプロパティ名は必ずユニークなので対象プロパティが確定する - const selectedProp = Object.entries(selectedDb.properties) - .find(([_, prop]) => { - // prompt result => "prperty_name " - return prop.name == promptSelectFilterPropResult.property.split(" <")[0] - }) + const selectedProp = Object.entries(selectedDb.properties).find(([_, prop]) => { + // prompt result => "prperty_name " + return prop.name == promptSelectFilterPropResult.property.split(' <')[0] + }) if (process.env.DEBUG) { - console.dir(selectedProp[1], {depth: null}) + console.dir(selectedProp[1], { depth: null }) } if (selectedProp[1].type == undefined) { - this.logToStderr("selectedProp.type is undefined") + this.logToStderr('selectedProp.type is undefined') return } const fieldChoices = await getFilterFields(selectedProp[1].type) if (fieldChoices == null) { - this.logToStderr("selected property is not supported to filter") + this.logToStderr('selected property is not supported to filter') continue } - const promptFieldResult = await prompts([{ - message: 'Select a field of filter', - type: 'autocomplete', - name: 'value', - choices: fieldChoices - }], { onCancel }) + const promptFieldResult = await prompts( + [ + { + message: 'Select a field of filter', + type: 'autocomplete', + name: 'value', + choices: fieldChoices, + }, + ], + { onCancel } + ) const filterField = promptFieldResult.value if (process.env.DEBUG) { console.log(`filterField: ${filterField}`) @@ -187,7 +208,7 @@ export default class DbQuery extends Command { filterValue ) if (filterObj == null) { - this.logToStderr("buildDatabaseQueryFilter error") + this.logToStderr('buildDatabaseQueryFilter error') this.exit(1) } @@ -201,39 +222,49 @@ export default class DbQuery extends Command { console.log(filter) } - const promptConfirmAddFilterFinishResult = await prompts([{ - message: 'Finish add filter?', - type: 'confirm', - name: 'value', - initial: true - }], { onCancel }) + const promptConfirmAddFilterFinishResult = await prompts( + [ + { + message: 'Finish add filter?', + type: 'confirm', + name: 'value', + initial: true, + }, + ], + { onCancel } + ) if (promptConfirmAddFilterFinishResult.value) { break } } } - } catch(e) { - this.error(e, {exit: 1}) + } catch (e) { + this.error(e, { exit: 1 }) } - if (filter != undefined && (flags.rawFilter == undefined && flags.fileFilter == undefined)) { - console.log("") - console.log("Filter:") - console.dir(filter, {depth: null}) - console.log("") + if (filter != undefined && flags.rawFilter == undefined && flags.fileFilter == undefined) { + console.log('') + console.log('Filter:') + console.dir(filter, { depth: null }) + console.log('') - const promptConfirmSaveFilterResult = await prompts([{ - message: 'Save this filter to a file?', - type: 'confirm', - name: 'value', - initial: false - }], { onCancel }) + const promptConfirmSaveFilterResult = await prompts( + [ + { + message: 'Save this filter to a file?', + type: 'confirm', + name: 'value', + initial: false, + }, + ], + { onCancel } + ) if (promptConfirmSaveFilterResult.value) { const promptFileNameResult = await prompts({ message: 'Filename', type: 'text', name: 'filename', - initial: dayjs().format('YYYYMMDD_HHmmss') - }); + initial: dayjs().format('YYYYMMDD_HHmmss'), + }) const fileName = `${promptFileNameResult.filename}.json` fs.writeFileSync(fileName, JSON.stringify(filter, null, 2)) this.logToStderr(`Saved to ${fileName}\n`) @@ -262,7 +293,7 @@ export default class DbQuery extends Command { } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table(res, columns, options) } diff --git a/src/commands/db/retrieve.ts b/src/commands/db/retrieve.ts index 9f53050..206038b 100644 --- a/src/commands/db/retrieve.ts +++ b/src/commands/db/retrieve.ts @@ -1,16 +1,9 @@ -import {Args, Command, Flags, ux} from '@oclif/core' -import { - DatabaseObjectResponse, -} from '@notionhq/client/build/src/api-endpoints' +import { Args, Command, Flags, ux } from '@oclif/core' +import { DatabaseObjectResponse } from '@notionhq/client/build/src/api-endpoints' import * as notion from '../../notion' -import { - onCancel, - getDbChoices, - outputRawJson, - getDbTitle, -} from '../../helper' +import { onCancel, getDbChoices, outputRawJson, getDbTitle } from '../../helper' -const prompts = require('prompts') +const prompts = require('prompts') export default class DbRetrieve extends Command { static description = 'Retrieve a database' @@ -25,7 +18,7 @@ export default class DbRetrieve extends Command { { description: 'Retrieve a database via database_id', command: 'notion-cli db retrieve DATABSE_ID', - } + }, ] static args = { @@ -46,12 +39,17 @@ export default class DbRetrieve extends Command { let databaseId = args.database_id if (databaseId == undefined) { const dbChoices = await getDbChoices() - const promptSelectedDbResult = await prompts([{ - type: 'autocomplete', - name: 'database_id', - message: 'Select a database', - choices: dbChoices - }], { onCancel }) + const promptSelectedDbResult = await prompts( + [ + { + type: 'autocomplete', + name: 'database_id', + message: 'Select a database', + choices: dbChoices, + }, + ], + { onCancel } + ) if (process.env.DEBUG) { console.log(promptSelectedDbResult) } @@ -77,7 +75,7 @@ export default class DbRetrieve extends Command { } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/db/update.ts b/src/commands/db/update.ts index 96e9c3a..c0b8e39 100644 --- a/src/commands/db/update.ts +++ b/src/commands/db/update.ts @@ -1,18 +1,13 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import { UpdateDatabaseParameters, DatabaseObjectResponse, } from '@notionhq/client/build/src/api-endpoints' -import { isFullDatabase } from '@notionhq/client'; +import { isFullDatabase } from '@notionhq/client' import * as notion from '../../notion' -import { - onCancel, - getDbChoices, - outputRawJson, - getDbTitle, -} from '../../helper' +import { onCancel, getDbChoices, outputRawJson, getDbTitle } from '../../helper' -const prompts = require('prompts') +const prompts = require('prompts') export default class DbUpdate extends Command { static description = 'Update a database' @@ -35,7 +30,7 @@ export default class DbUpdate extends Command { { description: 'Update a database with a specific database_id and output raw json', command: `$ notion-cli db update DATABASE_ID -r`, - } + }, ] static args = { @@ -45,7 +40,7 @@ export default class DbUpdate extends Command { static flags = { title: Flags.string({ char: 't', - description: 'New database title' + description: 'New database title', }), raw: Flags.boolean({ char: 'r', @@ -60,23 +55,33 @@ export default class DbUpdate extends Command { let databaseId = args.database_id if (databaseId == undefined) { const dbChoices = await getDbChoices() - const promptSelectedDbResult = await prompts([{ - type: 'autocomplete', - name: 'database_id', - message: 'Select a database to update', - choices: dbChoices - }], { onCancel }) + const promptSelectedDbResult = await prompts( + [ + { + type: 'autocomplete', + name: 'database_id', + message: 'Select a database to update', + choices: dbChoices, + }, + ], + { onCancel } + ) console.log(promptSelectedDbResult) databaseId = promptSelectedDbResult.database_id } let dbTitle = flags.title if (dbTitle == undefined) { - const dbPropPromptResult = await prompts([{ - type: 'text', - name: 'title', - message: 'Please input new database title', - }], { onCancel }) + const dbPropPromptResult = await prompts( + [ + { + type: 'text', + name: 'title', + message: 'Please input new database title', + }, + ], + { onCancel } + ) dbTitle = dbPropPromptResult.title } @@ -88,9 +93,9 @@ export default class DbUpdate extends Command { type: 'text', text: { content: dbTitle, - } - } - ] + }, + }, + ], } const res = await notion.updateDb(dbProps) @@ -112,7 +117,7 @@ export default class DbUpdate extends Command { } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/page/create.ts b/src/commands/page/create.ts index 0fff3ad..101e47f 100644 --- a/src/commands/page/create.ts +++ b/src/commands/page/create.ts @@ -1,4 +1,4 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../notion' import * as fs from 'fs' import * as path from 'path' @@ -8,10 +8,7 @@ import { PageObjectResponse, BlockObjectRequest, } from '@notionhq/client/build/src/api-endpoints' -import { - getPageTitle, - outputRawJson - } from '../../helper' +import { getPageTitle, outputRawJson } from '../../helper' export default class PageCreate extends Command { static description = 'Create a page' @@ -40,9 +37,10 @@ export default class PageCreate extends Command { command: `$ notion-cli page create -f ./path/to/source.md -d PARENT_DB_ID`, }, { - description: 'Create a page with a specific source markdown file and output raw json with parent_page_id', + description: + 'Create a page with a specific source markdown file and output raw json with parent_page_id', command: `$ notion-cli page create -f ./path/to/source.md -p PARENT_PAGE_ID -r`, - } + }, ] static flags = { @@ -64,7 +62,7 @@ export default class PageCreate extends Command { } public async run(): Promise { - const {args, flags} = await this.parse(PageCreate) + const { args, flags } = await this.parse(PageCreate) let pageProps: CreatePageParameters let pageParent: CreatePageParameters['parent'] @@ -89,7 +87,7 @@ export default class PageCreate extends Command { parent: pageParent, properties: { Name: { - title: [{ text: {content: fileName} }], + title: [{ text: { content: fileName } }], }, }, children: blocks as BlockObjectRequest[], @@ -97,7 +95,7 @@ export default class PageCreate extends Command { } else { pageProps = { parent: pageParent, - properties: {} + properties: {}, } } const res = await notion.createPage(pageProps) @@ -118,7 +116,7 @@ export default class PageCreate extends Command { } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/page/retrieve.ts b/src/commands/page/retrieve.ts index 20f89c0..6a2bf05 100644 --- a/src/commands/page/retrieve.ts +++ b/src/commands/page/retrieve.ts @@ -1,14 +1,8 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../notion' -import { - GetPageParameters, - PageObjectResponse, -} from '@notionhq/client/build/src/api-endpoints' -import { - getPageTitle, - outputRawJson -} from '../../helper' -import { NotionToMarkdown } from "notion-to-md" +import { GetPageParameters, PageObjectResponse } from '@notionhq/client/build/src/api-endpoints' +import { getPageTitle, outputRawJson } from '../../helper' +import { NotionToMarkdown } from 'notion-to-md' export default class PageRetrieve extends Command { static description = 'Retrieve a page' @@ -45,7 +39,7 @@ export default class PageRetrieve extends Command { static flags = { filter_properties: Flags.string({ char: 'p', - description: 'Comma separated property id string' + description: 'Comma separated property id string', }), raw: Flags.boolean({ char: 'r', @@ -59,7 +53,7 @@ export default class PageRetrieve extends Command { } public async run(): Promise { - const {args, flags} = await this.parse(PageRetrieve) + const { args, flags } = await this.parse(PageRetrieve) if (flags.markdown) { const n2m = new NotionToMarkdown({ notionClient: notion.client }) @@ -94,7 +88,7 @@ export default class PageRetrieve extends Command { } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/page/retrieve/property_item.ts b/src/commands/page/retrieve/property_item.ts index ea59b7a..38ca75d 100644 --- a/src/commands/page/retrieve/property_item.ts +++ b/src/commands/page/retrieve/property_item.ts @@ -1,4 +1,4 @@ -import {Args, Command} from '@oclif/core' +import { Args, Command } from '@oclif/core' import * as notion from '../../../notion' import { outputRawJson } from '../../../helper' @@ -24,7 +24,7 @@ export default class PageRetrievePropertyItem extends Command { } public async run(): Promise { - const {args} = await this.parse(PageRetrievePropertyItem) + const { args } = await this.parse(PageRetrievePropertyItem) const res = await notion.retrievePageProperty(args.page_id, args.property_id) outputRawJson(res) diff --git a/src/commands/page/update.ts b/src/commands/page/update.ts index 1c09dcf..1d5d454 100644 --- a/src/commands/page/update.ts +++ b/src/commands/page/update.ts @@ -1,13 +1,7 @@ -import {Args, Command, Flags, ux} from '@oclif/core' +import { Args, Command, Flags, ux } from '@oclif/core' import * as notion from '../../notion' -import { - UpdatePageParameters, - PageObjectResponse, -} from '@notionhq/client/build/src/api-endpoints' -import { - getPageTitle, - outputRawJson - } from '../../helper' +import { UpdatePageParameters, PageObjectResponse } from '@notionhq/client/build/src/api-endpoints' +import { getPageTitle, outputRawJson } from '../../helper' export default class PageUpdate extends Command { static description = 'Update a page' @@ -38,7 +32,7 @@ export default class PageUpdate extends Command { { description: 'Update a page and un archive and output raw json', command: `$ notion-cli page update PAGE_ID -u -r`, - } + }, ] static args = { @@ -46,8 +40,8 @@ export default class PageUpdate extends Command { } static flags = { - archived: Flags.boolean({ char: 'a'}), - un_archive: Flags.boolean({ char: 'u'}), + archived: Flags.boolean({ char: 'a' }), + un_archive: Flags.boolean({ char: 'u' }), raw: Flags.boolean({ char: 'r', description: 'output raw json', @@ -58,7 +52,7 @@ export default class PageUpdate extends Command { // NOTE: Support only archived or un archive property for now // TODO: Add support for updating a page properties, icon, cover public async run(): Promise { - const {args, flags} = await this.parse(PageUpdate) + const { args, flags } = await this.parse(PageUpdate) const pageProps: UpdatePageParameters = { page_id: args.page_id, } @@ -86,7 +80,7 @@ export default class PageUpdate extends Command { } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/search.ts b/src/commands/search.ts index b91f2e9..4027a94 100644 --- a/src/commands/search.ts +++ b/src/commands/search.ts @@ -1,16 +1,12 @@ -import {Command, Flags, ux} from '@oclif/core' +import { Command, Flags, ux } from '@oclif/core' import * as notion from '../notion' import { PageObjectResponse, SearchParameters, DatabaseObjectResponse, -} from '@notionhq/client/build/src/api-endpoints'; -import { isFullDatabase, isFullPage } from '@notionhq/client'; -import { - getDbTitle, - getPageTitle, - outputRawJson - } from '../helper' +} from '@notionhq/client/build/src/api-endpoints' +import { isFullDatabase, isFullPage } from '@notionhq/client' +import { getDbTitle, getPageTitle, outputRawJson } from '../helper' export default class Search extends Command { static description = 'Search by title' @@ -37,17 +33,20 @@ export default class Search extends Command { command: `$ notion-cli search -q 'My Page' --columns=title,object -d asc`, }, { - description: 'Search by title and output table with specific columns and sort direction and page size', + description: + 'Search by title and output table with specific columns and sort direction and page size', command: `$ notion-cli search -q 'My Page' -columns=title,object -d asc -s 10`, }, { - description: 'Search by title and output table with specific columns and sort direction and page size and start cursor', + description: + 'Search by title and output table with specific columns and sort direction and page size and start cursor', command: `$ notion-cli search -q 'My Page' --columns=title,object -d asc -s 10 -c START_CURSOR_ID`, }, { - description: 'Search by title and output table with specific columns and sort direction and page size and start cursor and property', + description: + 'Search by title and output table with specific columns and sort direction and page size and start cursor and property', command: `$ notion-cli search -q 'My Page' --columns=title,object -d asc -s 10 -c START_CURSOR_ID -p page`, - } + }, ] static flags = { @@ -58,7 +57,8 @@ export default class Search extends Command { sort_direction: Flags.string({ char: 'd', options: ['asc', 'desc'], - description: 'The direction to sort results. The only supported timestamp value is "last_edited_time"', + description: + 'The direction to sort results. The only supported timestamp value is "last_edited_time"', default: 'desc', }), property: Flags.string({ @@ -70,7 +70,8 @@ export default class Search extends Command { }), page_size: Flags.integer({ char: 's', - description: 'The number of results to return. The default is 5, with a minimum of 1 and a maximum of 100.', + description: + 'The number of results to return. The default is 5, with a minimum of 1 and a maximum of 100.', min: 1, max: 100, default: 5, @@ -138,7 +139,7 @@ export default class Search extends Command { } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table(res.results, columns, options) } diff --git a/src/commands/user/list.ts b/src/commands/user/list.ts index 4049349..9be3c91 100644 --- a/src/commands/user/list.ts +++ b/src/commands/user/list.ts @@ -1,7 +1,5 @@ -import {Command, Flags, ux} from '@oclif/core' -import { - UserObjectResponse -} from '@notionhq/client/build/src/api-endpoints' +import { Command, Flags, ux } from '@oclif/core' +import { UserObjectResponse } from '@notionhq/client/build/src/api-endpoints' import * as notion from '../../notion' import { outputRawJson } from '../../helper' @@ -18,7 +16,7 @@ export default class UserList extends Command { { description: 'List all users and output raw json', command: `$ notion-cli user list -r`, - } + }, ] static flags = { @@ -30,7 +28,7 @@ export default class UserList extends Command { } public async run(): Promise { - const {args, flags} = await this.parse(UserList) + const { args, flags } = await this.parse(UserList) const res = await notion.listUser() if (flags.raw) { outputRawJson(res) @@ -49,13 +47,13 @@ export default class UserList extends Command { return row.person } return row.bot - } + }, }, avatar_url: {}, } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table(res.results, columns, options) } diff --git a/src/commands/user/retrieve.ts b/src/commands/user/retrieve.ts index fb9f74d..0406fd6 100644 --- a/src/commands/user/retrieve.ts +++ b/src/commands/user/retrieve.ts @@ -1,7 +1,5 @@ -import { Args, Command, Flags, ux} from '@oclif/core' -import { - UserObjectResponse -} from '@notionhq/client/build/src/api-endpoints' +import { Args, Command, Flags, ux } from '@oclif/core' +import { UserObjectResponse } from '@notionhq/client/build/src/api-endpoints' import * as notion from '../../notion' import { outputRawJson } from '../../helper' @@ -18,7 +16,7 @@ export default class UserRetrieve extends Command { { description: 'Retrieve a user and output raw json', command: `$ notion-cli user retrieve USER_ID -r`, - } + }, ] static args = { @@ -34,7 +32,7 @@ export default class UserRetrieve extends Command { } public async run(): Promise { - const {args, flags} = await this.parse(UserRetrieve) + const { args, flags } = await this.parse(UserRetrieve) const res = await notion.retrieveUser(args.user_id) if (flags.raw) { outputRawJson(res) @@ -53,13 +51,13 @@ export default class UserRetrieve extends Command { return row.person } return row.bot - } + }, }, avatar_url: {}, } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/commands/user/retrieve/bot.ts b/src/commands/user/retrieve/bot.ts index 85bfba0..3431662 100644 --- a/src/commands/user/retrieve/bot.ts +++ b/src/commands/user/retrieve/bot.ts @@ -1,7 +1,5 @@ -import { Args, Command, Flags, ux} from '@oclif/core' -import { - UserObjectResponse -} from '@notionhq/client/build/src/api-endpoints' +import { Args, Command, Flags, ux } from '@oclif/core' +import { UserObjectResponse } from '@notionhq/client/build/src/api-endpoints' import * as notion from '../../../notion' import { outputRawJson } from '../../../helper' @@ -18,7 +16,7 @@ export default class UserRetrieveBot extends Command { { description: 'Retrieve a bot user and output raw json', command: `$ notion-cli user retrieve:bot -r`, - } + }, ] static args = {} @@ -32,7 +30,7 @@ export default class UserRetrieveBot extends Command { } public async run(): Promise { - const {args, flags} = await this.parse(UserRetrieveBot) + const { args, flags } = await this.parse(UserRetrieveBot) const res = await notion.botUser() if (flags.raw) { outputRawJson(res) @@ -51,13 +49,13 @@ export default class UserRetrieveBot extends Command { return row.person } return row.bot - } + }, }, avatar_url: {}, } const options = { printLine: this.log.bind(this), - ...flags + ...flags, } ux.table([res], columns, options) } diff --git a/src/helper.ts b/src/helper.ts index cede9eb..01eb6f1 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -14,19 +14,14 @@ export const outputRawJson = async (res: any) => { } export const onCancel = () => { - console.error('prompt is canceled'); + console.error('prompt is canceled') process.exit(0) } -export const getFilterFields = async ( - type: string -) => { +export const getFilterFields = async (type: string) => { switch (type) { case 'checkbox': - return [ - { title: 'equals' }, - { title: 'does_not_equal' }, - ] + return [{ title: 'equals' }, { title: 'does_not_equal' }] case 'created_time': case 'last_edited_time': case 'date': @@ -123,10 +118,7 @@ export const buildFilterPagePrompt = async ( type: 'autocomplete', name: 'value', message: 'select items', - choices: [ - { title: 'true' }, - { title: 'false' }, - ] + choices: [{ title: 'true' }, { title: 'false' }], } break case 'date': @@ -143,24 +135,24 @@ export const buildFilterPagePrompt = async ( initial: new Date(todayYear, todayMonth, todayDate, 0, 0, 0, 0), } break - case 'multi_select': - if (prop.multi_select.options == null) { - console.error("selected column options is null") - return - } - const multiSelectChoices = prop.multi_select.options.map((o) => { - return { - title: o.name, - value: o.name - } - }) - prompt = { - type: 'autocompleteMultiselect', - name: 'value', - message: 'select items', - choices: multiSelectChoices + case 'multi_select': + if (prop.multi_select.options == null) { + console.error('selected column options is null') + return + } + const multiSelectChoices = prop.multi_select.options.map((o) => { + return { + title: o.name, + value: o.name, } - break + }) + prompt = { + type: 'autocompleteMultiselect', + name: 'value', + message: 'select items', + choices: multiSelectChoices, + } + break case 'number': prompt = { type: 'number', @@ -173,17 +165,17 @@ export const buildFilterPagePrompt = async ( // console.log(relationPages) const relationChoices: IPromptChoice[] = [] for (const page of relationPages) { - if (page.object != "page") { + if (page.object != 'page') { continue } if (!isFullPage(page)) { continue } Object.entries(page.properties).forEach(([_, prop]) => { - if (prop.type == "title") { + if (prop.type == 'title') { relationChoices.push({ title: prop.title[0].plain_text, - value: page.id + value: page.id, }) return } @@ -193,7 +185,7 @@ export const buildFilterPagePrompt = async ( type: 'autocompleteMultiselect', name: 'value', message: 'select relation pages', - choices: relationChoices + choices: relationChoices, } break case 'rich_text': @@ -205,7 +197,7 @@ export const buildFilterPagePrompt = async ( } break case 'select': - const selectChoices = prop.select.options.map(o => { + const selectChoices = prop.select.options.map((o) => { return { title: o.name, } @@ -214,11 +206,11 @@ export const buildFilterPagePrompt = async ( type: 'autocomplete', name: 'value', message: 'select an item', - choices: selectChoices + choices: selectChoices, } break case 'status': - const statusChoices = prop.status.options.map(o => { + const statusChoices = prop.status.options.map((o) => { return { title: o.name, } @@ -227,7 +219,7 @@ export const buildFilterPagePrompt = async ( type: 'autocomplete', name: 'value', message: 'select an item', - choices: statusChoices + choices: statusChoices, } break @@ -246,7 +238,7 @@ export const buildDatabaseQueryFilter = async ( type: string, field: string, value: string | string[] | boolean -): Promise => { +): Promise => { let filter = null switch (type) { case 'checkbox': @@ -254,8 +246,8 @@ export const buildDatabaseQueryFilter = async ( property: name, [type]: { // boolean value - [field]: value == 'true' - } + [field]: value == 'true', + }, } break case 'date': @@ -269,8 +261,8 @@ export const buildDatabaseQueryFilter = async ( filter = { property: name, [type]: { - [field]: value - } + [field]: value, + }, } break case 'multi_select': @@ -280,8 +272,8 @@ export const buildDatabaseQueryFilter = async ( filter = { property: name, [type]: { - [field]: value[0] - } + [field]: value[0], + }, } } else { filter = { and: [] } @@ -289,8 +281,8 @@ export const buildDatabaseQueryFilter = async ( filter.and.push({ property: name, [type]: { - [field]: v - } + [field]: v, + }, }) } } @@ -310,33 +302,33 @@ export const buildPagePropUpdateData = async ( name: string, type: string, value: string -): Promise => { +): Promise => { switch (type) { case 'number': return { [name]: { - [type]: value - } + [type]: value, + }, } case 'select': return { [name]: { [type]: { - name: value - } - } + name: value, + }, + }, } case 'multi_select': const nameObjects = [] for (const val of value) { nameObjects.push({ - name: val + name: val, }) } return { [name]: { - [type]: nameObjects - } + [type]: nameObjects, + }, } case 'relation': const relationPageIds = [] @@ -345,43 +337,41 @@ export const buildPagePropUpdateData = async ( } return { [name]: { - [type]: relationPageIds - } + [type]: relationPageIds, + }, } } return null } -export const buildOneDepthJson = async ( - pages: QueryDatabaseResponse['results'], -) => { +export const buildOneDepthJson = async (pages: QueryDatabaseResponse['results']) => { const oneDepthJson = [] const relationJson = [] for (const page of pages) { - if (page.object != "page") { + if (page.object != 'page') { continue } if (!isFullPage(page)) { continue } const pageData = {} - pageData["page_id"] = page.id + pageData['page_id'] = page.id Object.entries(page.properties).forEach(([key, prop]) => { - switch(prop.type) { - case "number": + switch (prop.type) { + case 'number': pageData[key] = prop.number break - case "select": - pageData[key] = prop.select === null ? "" : prop.select.name + case 'select': + pageData[key] = prop.select === null ? '' : prop.select.name break - case "multi_select": + case 'multi_select': const multiSelects = [] for (const select of prop.multi_select) { multiSelects.push(select.name) } - pageData[key] = multiSelects.join(",") + pageData[key] = multiSelects.join(',') break - case "relation": + case 'relation': const relationPages = [] // relationJsonにkeyがなければ作成 if (relationJson[key] == null) { @@ -391,87 +381,87 @@ export const buildOneDepthJson = async ( relationPages.push(relation.id) relationJson[key].push({ page_id: page.id, - relation_page_id: relation.id + relation_page_id: relation.id, }) } - pageData[key] = relationPages.join(",") + pageData[key] = relationPages.join(',') break - case "created_time": + case 'created_time': pageData[key] = prop.created_time break - case "last_edited_time": + case 'last_edited_time': pageData[key] = prop.last_edited_time break - case "formula": + case 'formula': switch (prop.formula.type) { - case "string": + case 'string': pageData[key] = prop.formula.string break - case "number": + case 'number': pageData[key] = prop.formula.number break - case "boolean": + case 'boolean': pageData[key] = prop.formula.boolean break - case "date": + case 'date': pageData[key] = prop.formula.date.start break default: - // console.error(`${prop.formula.type} is not supported`) + // console.error(`${prop.formula.type} is not supported`) } break - case "url": + case 'url': pageData[key] = prop.url break - case "date": - pageData[key] = prop.date === null ? "" : prop.date.start + case 'date': + pageData[key] = prop.date === null ? '' : prop.date.start break - case "email": + case 'email': pageData[key] = prop.email break - case "phone_number": + case 'phone_number': pageData[key] = prop.phone_number break - case "created_by": + case 'created_by': pageData[key] = prop.created_by.id break - case "last_edited_by": + case 'last_edited_by': pageData[key] = prop.last_edited_by.id break - case "people": + case 'people': const people = [] for (const person of prop.people) { people.push(person.id) } - pageData[key] = people.join(",") + pageData[key] = people.join(',') break - case "files": + case 'files': const files = [] for (const file of prop.files) { files.push(file.name) } - pageData[key] = files.join(",") + pageData[key] = files.join(',') break - case "checkbox": + case 'checkbox': pageData[key] = prop.checkbox break // @ts-ignore - case "unique_id": + case 'unique_id': // @ts-ignore pageData[key] = `${prop.unique_id.prefix}-${prop.unique_id.number}` break - case "title": + case 'title': pageData[key] = prop.title[0].plain_text break - case "rich_text": + case 'rich_text': const richTexts = [] for (const richText of prop.rich_text) { richTexts.push(richText.plain_text) } - pageData[key] = richTexts.join(",") + pageData[key] = richTexts.join(',') break - case "status": - pageData[key] = prop.status === null ? "" : prop.status.name + case 'status': + pageData[key] = prop.status === null ? '' : prop.status.name break default: console.error(`${key}(type: ${prop.type}) is not supported`) @@ -480,14 +470,14 @@ export const buildOneDepthJson = async ( oneDepthJson.push(pageData) } - return {oneDepthJson, relationJson} + return { oneDepthJson, relationJson } } export const getDbChoices = async () => { const dbs = await notion.searchDb() const dbChoices = [] for (const db of dbs) { - if (db.object != "database") { + if (db.object != 'database') { continue } if (!isFullDatabase(db)) { @@ -501,25 +491,21 @@ export const getDbChoices = async () => { value: db.id, }) } - const sortedDbChoices = dbChoices.sort((a,b)=> { + const sortedDbChoices = dbChoices.sort((a, b) => { return a.title.localeCompare(b.title) }) return sortedDbChoices } -export const getDbTitle = ( - row: DatabaseObjectResponse -) => { +export const getDbTitle = (row: DatabaseObjectResponse) => { if (row.title && row.title.length > 0) { return row.title[0].plain_text } return 'Untitled' } -export const getPageTitle = ( - row: PageObjectResponse -) => { +export const getPageTitle = (row: PageObjectResponse) => { let title = 'Untitled' Object.entries(row.properties).find(([_, prop]) => { if (prop.type === 'title' && prop.title.length > 0) { @@ -530,9 +516,7 @@ export const getPageTitle = ( return title } -export const getBlockPlainText = ( - row: BlockObjectResponse -) => { +export const getBlockPlainText = (row: BlockObjectResponse) => { try { switch (row.type) { case 'bookmark': @@ -587,7 +571,7 @@ export const getBlockPlainText = ( default: return row[row.type] } - } catch(e) { + } catch (e) { console.error(`${row.type} is not supported`) console.error(e) return '' diff --git a/src/notion.ts b/src/notion.ts index 20a9ca2..cc1684d 100644 --- a/src/notion.ts +++ b/src/notion.ts @@ -31,7 +31,7 @@ export const queryDb = async ( const { results, next_cursor } = await client.databases.query({ database_id: databaseId, filter: f, - start_cursor: cursor + start_cursor: cursor, }) pages.push(...results) if (!next_cursor) { @@ -49,15 +49,11 @@ export const createDb = async ( return await client.databases.create(dbProps) } -export const updateDb = async ( - dbProps: UpdateDatabaseParameters -): Promise => { +export const updateDb = async (dbProps: UpdateDatabaseParameters): Promise => { return await client.databases.update(dbProps) } -export const retrieveDb = async ( - databaseId: string, -): Promise => { +export const retrieveDb = async (databaseId: string): Promise => { return await client.databases.retrieve({ database_id: databaseId }) } @@ -74,41 +70,34 @@ export const retrievePageProperty = async (pageId: string, propId: string) => { return res } -export const createPage = async ( - pageProps: CreatePageParameters, -) => { +export const createPage = async (pageProps: CreatePageParameters) => { return client.pages.create(pageProps) } -export const updatePageProps = async ( - pageParams: UpdatePageParameters, -) => { +export const updatePageProps = async (pageParams: UpdatePageParameters) => { return client.pages.update(pageParams) } // To keep the same page URL, // remove all blocks in the page and add new blocks -export const updatePage = async ( - pageId: string, - blocks: BlockObjectRequest[] -) => { +export const updatePage = async (pageId: string, blocks: BlockObjectRequest[]) => { const blks = await client.blocks.children.list({ block_id: pageId, - }); + }) for (const blk of blks.results) { await client.blocks.delete({ block_id: blk.id, - }); + }) } const res = await client.blocks.children.append({ block_id: pageId, // @ts-ignore children: blocks, - }); + }) return res -}; +} export const retrieveBlock = async (blockId: string) => { const res = client.blocks.retrieve({ @@ -128,9 +117,7 @@ export const retrieveBlockChildren = async (blockId: string) => { return res } -export const appendBlockChildren = async ( - params: AppendBlockChildrenParameters -) => { +export const appendBlockChildren = async (params: AppendBlockChildrenParameters) => { return client.blocks.children.append(params) } @@ -159,14 +146,12 @@ export const searchDb = async () => { const { results } = await client.search({ filter: { value: 'database', - property: 'object' - } + property: 'object', + }, }) return results } -export const search = async ( - params: SearchParameters -) => { +export const search = async (params: SearchParameters) => { return await client.search(params) } diff --git a/test/commands/block/append.test.ts b/test/commands/block/append.test.ts index 488a6f7..971dd94 100644 --- a/test/commands/block/append.test.ts +++ b/test/commands/block/append.test.ts @@ -1,11 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { return test - .nock('https://api.notion.com', api => api - .patch('/v1/blocks/dummy-block-id/children') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + .nock('https://api.notion.com', (api) => + api.patch('/v1/blocks/dummy-block-id/children').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -26,12 +26,12 @@ const response = { { type: 'text', plain_text: 'dummy-heading-2-content', - } - ] - } - } + }, + ], + }, + }, ], - next_cursor: "dummy-next-cursor", + next_cursor: 'dummy-next-cursor', has_more: false, type: 'block', block: {}, @@ -40,30 +40,30 @@ const response = { describe('block:append', () => { describe('shows ux.table result', () => { apiMock(response) - .command([ - 'block:append', - '--no-truncate', - 'dummy-block-id', - '{"type": "heading_2", "heading_2": {"rich_text": [{"type": "text", "plaoin_text": "dummy-heading-2-content"}]}}' - ]) - .it('shows deleted block object when success', ctx => { - expect(ctx.stdout).to.match(/Object.*Id.*Type.*Parent.*Content/) - expect(ctx.stdout).to.match(/block.*dummy-block-id.*heading_2.*dummy-heading-2-content/) - }) + .command([ + 'block:append', + '--no-truncate', + 'dummy-block-id', + '{"type": "heading_2", "heading_2": {"rich_text": [{"type": "text", "plaoin_text": "dummy-heading-2-content"}]}}', + ]) + .it('shows deleted block object when success', (ctx) => { + expect(ctx.stdout).to.match(/Object.*Id.*Type.*Parent.*Content/) + expect(ctx.stdout).to.match(/block.*dummy-block-id.*heading_2.*dummy-heading-2-content/) + }) }) describe('shows raw json result', () => { apiMock(response) - .command([ - 'block:append', - '--raw', - 'dummy-block-id', - '{"type": "heading_2", "heading_2": {"rich_text": [{"type": "text", "plaoin_text": "dummy-heading-2-content"}]}}' - ]) - .exit(0) - .it('shows updated block object when success', ctx => { - expect(ctx.stdout).to.contain("object\": \"list") - expect(ctx.stdout).to.contain("results\": [") - expect(ctx.stdout).to.contain("type\": \"heading_2") - }) + .command([ + 'block:append', + '--raw', + 'dummy-block-id', + '{"type": "heading_2", "heading_2": {"rich_text": [{"type": "text", "plaoin_text": "dummy-heading-2-content"}]}}', + ]) + .exit(0) + .it('shows updated block object when success', (ctx) => { + expect(ctx.stdout).to.contain('object": "list') + expect(ctx.stdout).to.contain('results": [') + expect(ctx.stdout).to.contain('type": "heading_2') + }) }) }) diff --git a/test/commands/block/delete.test.ts b/test/commands/block/delete.test.ts index 142a7db..a277802 100644 --- a/test/commands/block/delete.test.ts +++ b/test/commands/block/delete.test.ts @@ -1,11 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { return test - .nock('https://api.notion.com', api => api - .delete('/v1/blocks/dummy-block-id') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + .nock('https://api.notion.com', (api) => + api.delete('/v1/blocks/dummy-block-id').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -23,29 +23,29 @@ const response = { { type: 'text', plain_text: 'dummy-heading-2-content', - } - ] - } + }, + ], + }, } describe('block:delete', () => { describe('shows ux.table result', () => { apiMock(response) - .command(['block:delete', 'dummy-block-id']) - .it('shows deleted block object when success', ctx => { - expect(ctx.stdout).to.match(/Object.*Id.*Type.*Parent.*Content/) - expect(ctx.stdout).to.match(/block.*dummy-block-id.*heading_2.*dummy-heading-2-content/) - }) + .command(['block:delete', 'dummy-block-id']) + .it('shows deleted block object when success', (ctx) => { + expect(ctx.stdout).to.match(/Object.*Id.*Type.*Parent.*Content/) + expect(ctx.stdout).to.match(/block.*dummy-block-id.*heading_2.*dummy-heading-2-content/) + }) }) describe('shows raw json result', () => { apiMock(response) - .command(['block:delete', 'dummy-block-id', '--raw']) - .exit(0) - .it('shows deleted block object when success', ctx => { - expect(ctx.stdout).to.contain("object\": \"block") - expect(ctx.stdout).to.contain("id\": \"dummy-block-id") - expect(ctx.stdout).to.contain("archived\": true") - }) + .command(['block:delete', 'dummy-block-id', '--raw']) + .exit(0) + .it('shows deleted block object when success', (ctx) => { + expect(ctx.stdout).to.contain('object": "block') + expect(ctx.stdout).to.contain('id": "dummy-block-id') + expect(ctx.stdout).to.contain('archived": true') + }) }) }) diff --git a/test/commands/block/retrieve.test.ts b/test/commands/block/retrieve.test.ts index 7759e77..0684330 100644 --- a/test/commands/block/retrieve.test.ts +++ b/test/commands/block/retrieve.test.ts @@ -1,11 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { return test - .nock('https://api.notion.com', api => api - .get('/v1/blocks/dummy-block-id') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + .nock('https://api.notion.com', (api) => + api.get('/v1/blocks/dummy-block-id').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -20,26 +20,26 @@ const response = { type: 'child_page', child_page: { title: 'dummy child page title', - } + }, } describe('block:retrieve', () => { describe('shows ux.table result', () => { apiMock(response) - .command(['block:retrieve', 'dummy-block-id']) - .it('shows retrieved block object when success', ctx => { - expect(ctx.stdout).to.match(/Object.*Id.*Type.*Parent.*Content/) - expect(ctx.stdout).to.match(/block.*dummy-block-id.*child_page.*dummy child page title/) - }) + .command(['block:retrieve', 'dummy-block-id']) + .it('shows retrieved block object when success', (ctx) => { + expect(ctx.stdout).to.match(/Object.*Id.*Type.*Parent.*Content/) + expect(ctx.stdout).to.match(/block.*dummy-block-id.*child_page.*dummy child page title/) + }) }) describe('shows raw json result', () => { apiMock(response) - .command(['block:retrieve', 'dummy-block-id', '--raw']) - .exit(0) - .it('shows retrieved block object when success', ctx => { - expect(ctx.stdout).to.contain("object\": \"block") - expect(ctx.stdout).to.contain("type\": \"child_page") - }) + .command(['block:retrieve', 'dummy-block-id', '--raw']) + .exit(0) + .it('shows retrieved block object when success', (ctx) => { + expect(ctx.stdout).to.contain('object": "block') + expect(ctx.stdout).to.contain('type": "child_page') + }) }) }) diff --git a/test/commands/block/retrieve/children.test.ts b/test/commands/block/retrieve/children.test.ts index 163470a..74cef4c 100644 --- a/test/commands/block/retrieve/children.test.ts +++ b/test/commands/block/retrieve/children.test.ts @@ -1,11 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { return test - .nock('https://api.notion.com', api => api - .get('/v1/blocks/dummy-block-id/children') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + .nock('https://api.notion.com', (api) => + api.get('/v1/blocks/dummy-block-id/children').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -26,12 +26,12 @@ const response = { { type: 'text', plain_text: 'dummy-heading-2-content', - } - ] - } - } + }, + ], + }, + }, ], - next_cursor: "dummy-next-cursor", + next_cursor: 'dummy-next-cursor', has_more: false, type: 'block', block: {}, @@ -40,20 +40,20 @@ const response = { describe('block:retrieve:children', () => { describe('shows ux.table result', () => { apiMock(response) - .command(['block:retrieve:children', 'dummy-block-id', '--no-truncate']) - .it('shows deleted block object when success', ctx => { - expect(ctx.stdout).to.match(/Object.*Id.*Type.*Content/) - expect(ctx.stdout).to.match(/block.*dummy-block-id.*heading_2.*dummy-heading-2-content/) - }) + .command(['block:retrieve:children', 'dummy-block-id', '--no-truncate']) + .it('shows deleted block object when success', (ctx) => { + expect(ctx.stdout).to.match(/Object.*Id.*Type.*Content/) + expect(ctx.stdout).to.match(/block.*dummy-block-id.*heading_2.*dummy-heading-2-content/) + }) }) describe('shows raw json result', () => { apiMock(response) - .command(['block:retrieve:children', 'dummy-block-id', '--raw']) - .exit(0) - .it('shows updated block object when success', ctx => { - expect(ctx.stdout).to.contain("object\": \"list") - expect(ctx.stdout).to.contain("results\": [") - expect(ctx.stdout).to.contain("type\": \"block") - }) + .command(['block:retrieve:children', 'dummy-block-id', '--raw']) + .exit(0) + .it('shows updated block object when success', (ctx) => { + expect(ctx.stdout).to.contain('object": "list') + expect(ctx.stdout).to.contain('results": [') + expect(ctx.stdout).to.contain('type": "block') + }) }) }) diff --git a/test/commands/block/update.test.ts b/test/commands/block/update.test.ts index 89b5213..8c444be 100644 --- a/test/commands/block/update.test.ts +++ b/test/commands/block/update.test.ts @@ -1,11 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { return test - .nock('https://api.notion.com', api => api - .patch('/v1/blocks/dummy-block-id') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + .nock('https://api.notion.com', (api) => + api.patch('/v1/blocks/dummy-block-id').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -23,28 +23,28 @@ const response = { { type: 'text', plain_text: 'dummy-heading-2-content', - } - ] - } + }, + ], + }, } describe('block:update', () => { describe('shows ux.table result', () => { apiMock(response) - .command(['block:update', 'dummy-block-id', '--no-truncate']) - .it('shows deleted block object when success', ctx => { - expect(ctx.stdout).to.match(/Object.*Id.*Type.*Parent.*Content/) - expect(ctx.stdout).to.match(/block.*dummy-block-id.*heading_2.*dummy-heading-2-content/) - }) + .command(['block:update', 'dummy-block-id', '--no-truncate']) + .it('shows deleted block object when success', (ctx) => { + expect(ctx.stdout).to.match(/Object.*Id.*Type.*Parent.*Content/) + expect(ctx.stdout).to.match(/block.*dummy-block-id.*heading_2.*dummy-heading-2-content/) + }) }) describe('shows raw json result', () => { apiMock(response) - .command(['block:update', 'dummy-block-id', '--raw']) - .exit(0) - .it('shows updated block object when success', ctx => { - expect(ctx.stdout).to.contain("object\": \"block") - expect(ctx.stdout).to.contain("id\": \"dummy-block-id") - expect(ctx.stdout).to.contain("archived\": true") - }) + .command(['block:update', 'dummy-block-id', '--raw']) + .exit(0) + .it('shows updated block object when success', (ctx) => { + expect(ctx.stdout).to.contain('object": "block') + expect(ctx.stdout).to.contain('id": "dummy-block-id') + expect(ctx.stdout).to.contain('archived": true') + }) }) }) diff --git a/test/commands/db/create.test.ts b/test/commands/db/create.test.ts index be8c374..866b12c 100644 --- a/test/commands/db/create.test.ts +++ b/test/commands/db/create.test.ts @@ -1,10 +1,9 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { - return test.nock('https://api.notion.com', api => api - .post('/v1/databases') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + return test + .nock('https://api.notion.com', (api) => api.post('/v1/databases').reply(200, response)) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } describe('db:create', () => { @@ -18,38 +17,30 @@ describe('db:create', () => { content: 'dummy database title', }, plain_text: 'dummy database title', - } + }, ], url: 'https://www.notion.so/dummy-database-id', } describe('with no flags', () => { apiMock(response) - .command([ - 'db:create', - '--no-truncate', - '-t', 'dummy database title', - 'dummy-page-id' - ]) - .it('shows created result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy database title.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/) - }) + .command(['db:create', '--no-truncate', '-t', 'dummy database title', 'dummy-page-id']) + .it('shows created result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy database title.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/ + ) + }) }) describe('with --raw flags', () => { apiMock(response) - .command([ - 'db:create', - 'dummy-page-id', - '-t', 'dummy database title', - '--raw' - ]) - .exit(0) - .it('shows created database object when success with title flags', ctx => { - expect(ctx.stdout).to.contain("dummy-database-id") - expect(ctx.stdout).to.contain("dummy database title") - }) + .command(['db:create', 'dummy-page-id', '-t', 'dummy database title', '--raw']) + .exit(0) + .it('shows created database object when success with title flags', (ctx) => { + expect(ctx.stdout).to.contain('dummy-database-id') + expect(ctx.stdout).to.contain('dummy database title') + }) }) describe('response title is []', () => { @@ -61,15 +52,12 @@ describe('db:create', () => { } apiMock(titleEmptyResponse) - .command([ - 'db:create', - '--no-truncate', - '-t', 'dummy database title', - 'dummy-page-id' - ]) - .it('shows created result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/) - }) + .command(['db:create', '--no-truncate', '-t', 'dummy database title', 'dummy-page-id']) + .it('shows created result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/ + ) + }) }) }) diff --git a/test/commands/db/query.test.ts b/test/commands/db/query.test.ts index 4114da5..d8fab86 100644 --- a/test/commands/db/query.test.ts +++ b/test/commands/db/query.test.ts @@ -1,11 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { - return test.nock('https://api.notion.com', api => api - .post('/v1/databases/dummy-database-id/query') - .reply(200, response) - ) - .stdout({print: process.env.TEST_DEBUG ? true : false}) + return test + .nock('https://api.notion.com', (api) => + api.post('/v1/databases/dummy-database-id/query').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -25,12 +25,12 @@ const response = { content: 'dummy page title', }, plain_text: 'dummy page title', - } + }, ], - } + }, }, url: 'https://www.notion.so/dummy-page-id', - } + }, ], } @@ -45,52 +45,43 @@ const titleEmptyResponse = { id: 'title', type: 'title', title: [], - } + }, }, url: 'https://www.notion.so/dummy-page-id', - } + }, ], } describe('db:query', () => { describe('with raw filter flags', () => { apiMock(response) - .command([ - 'db:query', - 'dummy-database-id', - '-a', '{"and": []}', - ]) - .it('shows query result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['db:query', 'dummy-database-id', '-a', '{"and": []}']) + .it('shows query result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) }) describe('with --raw flags', () => { apiMock(response) - .command([ - 'db:query', - 'dummy-database-id', - '-a', '{"and": []}', - '--raw', - ]) - .exit(0) - .it('shows query result page objects', ctx => { - expect(ctx.stdout).to.contain("dummy-page-id") - expect(ctx.stdout).to.contain("dummy page title") - }) + .command(['db:query', 'dummy-database-id', '-a', '{"and": []}', '--raw']) + .exit(0) + .it('shows query result page objects', (ctx) => { + expect(ctx.stdout).to.contain('dummy-page-id') + expect(ctx.stdout).to.contain('dummy page title') + }) }) describe('return title is []', () => { apiMock(titleEmptyResponse) - .command([ - 'db:query', - 'dummy-database-id', - '-a', '{"and": []}', - ]) - .it('shows query result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['db:query', 'dummy-database-id', '-a', '{"and": []}']) + .it('shows query result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) }) }) diff --git a/test/commands/db/retrieve.test.ts b/test/commands/db/retrieve.test.ts index 2e0c3db..a007b2c 100644 --- a/test/commands/db/retrieve.test.ts +++ b/test/commands/db/retrieve.test.ts @@ -1,10 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { - return test.nock('https://api.notion.com', api => api - .get('/v1/databases/dummy-database-id') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + return test + .nock('https://api.notion.com', (api) => + api.get('/v1/databases/dummy-database-id').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -17,7 +18,7 @@ const response = { content: 'dummy database title', }, plain_text: 'dummy database title', - } + }, ], url: 'https://www.notion.so/dummy-database-id', } @@ -32,41 +33,33 @@ const titleEmptyResponse = { describe('db:retrieve', () => { describe('with no flags', () => { apiMock(response) - .command([ - 'db:retrieve', - '--no-truncate', - 'dummy-database-id' - ]) - .it('shows retrieved result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy database title.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/) - }) + .command(['db:retrieve', '--no-truncate', 'dummy-database-id']) + .it('shows retrieved result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy database title.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/ + ) + }) }) describe('with --raw flags', () => { apiMock(response) - .command([ - 'db:retrieve', - 'dummy-database-id', - '--raw' - ]) - .exit(0) - .it('shows a database object', ctx => { - expect(ctx.stdout).to.contain("dummy-database-id") - expect(ctx.stdout).to.contain("dummy database title") - }) + .command(['db:retrieve', 'dummy-database-id', '--raw']) + .exit(0) + .it('shows a database object', (ctx) => { + expect(ctx.stdout).to.contain('dummy-database-id') + expect(ctx.stdout).to.contain('dummy database title') + }) }) describe('response title is []', () => { apiMock(titleEmptyResponse) - .command([ - 'db:retrieve', - '--no-truncate', - 'dummy-database-id' - ]) - .it('shows retrieved result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/) - }) + .command(['db:retrieve', '--no-truncate', 'dummy-database-id']) + .it('shows retrieved result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/ + ) + }) }) }) diff --git a/test/commands/db/update.test.ts b/test/commands/db/update.test.ts index a8dcf6e..b6cf116 100644 --- a/test/commands/db/update.test.ts +++ b/test/commands/db/update.test.ts @@ -1,10 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { - return test.nock('https://api.notion.com', api => api - .patch('/v1/databases/dummy-database-id') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + return test + .nock('https://api.notion.com', (api) => + api.patch('/v1/databases/dummy-database-id').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -17,7 +18,7 @@ const response = { content: 'dummy database title', }, plain_text: 'dummy database title', - } + }, ], url: 'https://www.notion.so/dummy-database-id', } @@ -32,44 +33,33 @@ const titleEmptyResponse = { describe('db:update', () => { describe('with no flags', () => { apiMock(response) - .command([ - 'db:update', - '--no-truncate', - '-t dummy database title', - 'dummy-database-id' - ]) - .it('shows updated result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy database title.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/) - }) + .command(['db:update', '--no-truncate', '-t dummy database title', 'dummy-database-id']) + .it('shows updated result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy database title.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/ + ) + }) }) describe('with --raw flags', () => { apiMock(response) - .command([ - 'db:update', - '-t', 'dummy database title', - 'dummy-database-id', - '--raw' - ]) - .exit(0) - .it('shows updated database object', ctx => { - expect(ctx.stdout).to.contain("dummy-database-id") - expect(ctx.stdout).to.contain("dummy database title") - }) + .command(['db:update', '-t', 'dummy database title', 'dummy-database-id', '--raw']) + .exit(0) + .it('shows updated database object', (ctx) => { + expect(ctx.stdout).to.contain('dummy-database-id') + expect(ctx.stdout).to.contain('dummy database title') + }) }) describe('response title is []', () => { apiMock(titleEmptyResponse) - .command([ - 'db:update', - '--no-truncate', - '-t dummy database title', - 'dummy-database-id' - ]) - .it('shows updated result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/) - }) + .command(['db:update', '--no-truncate', '-t dummy database title', 'dummy-database-id']) + .it('shows updated result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/ + ) + }) }) }) diff --git a/test/commands/page/create.test.ts b/test/commands/page/create.test.ts index 2ff8737..cea96e2 100644 --- a/test/commands/page/create.test.ts +++ b/test/commands/page/create.test.ts @@ -1,10 +1,9 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { - return test.nock('https://api.notion.com', api => api - .post('/v1/pages') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + return test + .nock('https://api.notion.com', (api) => api.post('/v1/pages').reply(200, response)) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const createOnPageResponse = { @@ -26,9 +25,9 @@ const createOnPageResponse = { content: 'dummy page title', }, plain_text: 'dummy page title', - } - ] - } + }, + ], + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -46,7 +45,7 @@ const createOnPageResponseWithEmptyTitle = { id: 'title', type: 'title', title: [], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -70,9 +69,9 @@ const createOnDbResponse = { content: 'dummy page title', }, plain_text: 'dummy page title', - } + }, ], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -90,7 +89,7 @@ const createOnDbResponseWithEmptyTitle = { id: 'title', type: 'title', title: [], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -98,79 +97,63 @@ const createOnDbResponseWithEmptyTitle = { describe('page:create', () => { describe('with parent_page_id flags', () => { apiMock(createOnPageResponse) - .command([ - 'page:create', - '--no-truncate', - '-p','dummy-parent-page-id' - ]) - .it('shows create page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:create', '--no-truncate', '-p', 'dummy-parent-page-id']) + .it('shows create page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) describe('with --raw flags', () => { apiMock(createOnPageResponse) - .command([ - 'page:create', - '-p', 'dummy-parent-page-id', - '--raw' - ]) - .exit(0) - .it('shows a page object', ctx => { - expect(ctx.stdout).to.contain("dummy-parent-page-id") - }) + .command(['page:create', '-p', 'dummy-parent-page-id', '--raw']) + .exit(0) + .it('shows a page object', (ctx) => { + expect(ctx.stdout).to.contain('dummy-parent-page-id') + }) }) describe('response title is []', () => { apiMock(createOnPageResponseWithEmptyTitle) - .command([ - 'page:create', - '--no-truncate', - '-p','dummy-parent-page-id' - ]) - .it('shows create page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:create', '--no-truncate', '-p', 'dummy-parent-page-id']) + .it('shows create page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) }) }) describe('with parent_db_id flags', () => { apiMock(createOnDbResponse) - .command([ - 'page:create', - '--no-truncate', - '-p','dummy-parent-database-id' - ]) - .it('shows create page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:create', '--no-truncate', '-p', 'dummy-parent-database-id']) + .it('shows create page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) describe('with --raw flags', () => { apiMock(createOnDbResponse) - .command([ - 'page:create', - '-p', 'dummy-parent-database-id', - '--raw' - ]) - .exit(0) - .it('shows a page object', ctx => { - expect(ctx.stdout).to.contain("dummy-parent-database-id") - }) + .command(['page:create', '-p', 'dummy-parent-database-id', '--raw']) + .exit(0) + .it('shows a page object', (ctx) => { + expect(ctx.stdout).to.contain('dummy-parent-database-id') + }) }) describe('response title is []', () => { apiMock(createOnDbResponseWithEmptyTitle) - .command([ - 'page:create', - '--no-truncate', - '-p','dummy-parent-database-id' - ]) - .it('shows create page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:create', '--no-truncate', '-p', 'dummy-parent-database-id']) + .it('shows create page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) }) }) }) diff --git a/test/commands/page/retrieve.test.ts b/test/commands/page/retrieve.test.ts index c271389..945862e 100644 --- a/test/commands/page/retrieve.test.ts +++ b/test/commands/page/retrieve.test.ts @@ -1,10 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { - return test.nock('https://api.notion.com', api => api - .get('/v1/pages/dummy-page-id') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + return test + .nock('https://api.notion.com', (api) => + api.get('/v1/pages/dummy-page-id').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const retrieveOnPageResponse = { @@ -26,9 +27,9 @@ const retrieveOnPageResponse = { content: 'dummy page title', }, plain_text: 'dummy page title', - } + }, ], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -46,7 +47,7 @@ const retrieveOnPageResponseWithEmptyTitle = { id: 'title', type: 'title', title: [], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -70,9 +71,9 @@ const retrieveOnDbResponse = { content: 'dummy page title', }, plain_text: 'dummy page title', - } + }, ], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -90,7 +91,7 @@ const retrieveOnDbResponseWIthEmtyTitle = { id: 'title', type: 'title', title: [], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -98,75 +99,67 @@ const retrieveOnDbResponseWIthEmtyTitle = { describe('page:retrieve', () => { describe('with page_id on a page flags', () => { apiMock(retrieveOnPageResponse) - .command([ - 'page:retrieve', - '--no-truncate', - 'dummy-page-id' - ]) - .it('shows retrieve page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:retrieve', '--no-truncate', 'dummy-page-id']) + .it('shows retrieve page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) describe('with --raw flags', () => { apiMock(retrieveOnPageResponse) - .command([ - 'page:retrieve', - 'dummy-page-id', - '--raw' - ]) - .exit(0) - .it('shows a page object', ctx => { - expect(ctx.stdout).to.contain("object\": \"page") - expect(ctx.stdout).to.contain("id\": \"dummy-page-id") - expect(ctx.stdout).to.contain("url\": \"https://www.notion.so/dummy-page-id") - }) + .command(['page:retrieve', 'dummy-page-id', '--raw']) + .exit(0) + .it('shows a page object', (ctx) => { + expect(ctx.stdout).to.contain('object": "page') + expect(ctx.stdout).to.contain('id": "dummy-page-id') + expect(ctx.stdout).to.contain('url": "https://www.notion.so/dummy-page-id') + }) }) describe('response title is []', () => { apiMock(retrieveOnPageResponseWithEmptyTitle) - .command([ - 'page:retrieve', - '--no-truncate', - 'dummy-page-id' - ]) - .it('shows retrieve page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:retrieve', '--no-truncate', 'dummy-page-id']) + .it('shows retrieve page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) }) }) describe('with page_id on a db flags', () => { apiMock(retrieveOnDbResponse) - .command(['page:retrieve', '--no-truncate', 'dummy-page-id']) - .it('shows retrieve page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:retrieve', '--no-truncate', 'dummy-page-id']) + .it('shows retrieve page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) describe('with --raw flags', () => { apiMock(retrieveOnDbResponse) - .command([ - 'page:retrieve', - 'dummy-page-id', - '--raw' - ]) - .exit(0) - .it('shows a page object', ctx => { - expect(ctx.stdout).to.contain("object\": \"page") - expect(ctx.stdout).to.contain("id\": \"dummy-page-id") - expect(ctx.stdout).to.contain("url\": \"https://www.notion.so/dummy-page-id") - }) + .command(['page:retrieve', 'dummy-page-id', '--raw']) + .exit(0) + .it('shows a page object', (ctx) => { + expect(ctx.stdout).to.contain('object": "page') + expect(ctx.stdout).to.contain('id": "dummy-page-id') + expect(ctx.stdout).to.contain('url": "https://www.notion.so/dummy-page-id') + }) }) describe('response title is [', () => { apiMock(retrieveOnDbResponseWIthEmtyTitle) - .command(['page:retrieve', '--no-truncate', 'dummy-page-id']) - .it('shows retrieve page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:retrieve', '--no-truncate', 'dummy-page-id']) + .it('shows retrieve page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) }) }) }) diff --git a/test/commands/page/retrieve/property_item.test.ts b/test/commands/page/retrieve/property_item.test.ts index e995143..23fdeb6 100644 --- a/test/commands/page/retrieve/property_item.test.ts +++ b/test/commands/page/retrieve/property_item.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' describe('page:retrieve:property_item', () => { const response = { @@ -13,7 +13,7 @@ describe('page:retrieve:property_item', () => { content: 'dummy title', link: null, }, - } + }, }, ], next_cursor: null, @@ -22,18 +22,13 @@ describe('page:retrieve:property_item', () => { } test - .nock('https://api.notion.com', api => api - .get('/v1/pages/dummy-page-id/properties/dummy-property-id') - .reply(200, response) - ) - .stdout() - .command([ - 'page:retrieve:property_item', - 'dummy-page-id', - 'dummy-property-id' - ]) - .it('shows retrieved page object when success', ctx => { - expect(ctx.stdout).to.contain("object\": \"list") - expect(ctx.stdout).to.contain("type\": \"property_item") - }) + .nock('https://api.notion.com', (api) => + api.get('/v1/pages/dummy-page-id/properties/dummy-property-id').reply(200, response) + ) + .stdout() + .command(['page:retrieve:property_item', 'dummy-page-id', 'dummy-property-id']) + .it('shows retrieved page object when success', (ctx) => { + expect(ctx.stdout).to.contain('object": "list') + expect(ctx.stdout).to.contain('type": "property_item') + }) }) diff --git a/test/commands/page/update.test.ts b/test/commands/page/update.test.ts index c935fbb..1fdc419 100644 --- a/test/commands/page/update.test.ts +++ b/test/commands/page/update.test.ts @@ -1,10 +1,11 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { - return test.nock('https://api.notion.com', api => api - .patch('/v1/pages/dummy-page-id') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + return test + .nock('https://api.notion.com', (api) => + api.patch('/v1/pages/dummy-page-id').reply(200, response) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const responseOnPage = { @@ -19,14 +20,16 @@ const responseOnPage = { title: { id: 'title', type: 'title', - title: [{ - type: 'text', - text: { - content: 'dummy page title', + title: [ + { + type: 'text', + text: { + content: 'dummy page title', + }, + plain_text: 'dummy page title', }, - plain_text: 'dummy page title', - }], - } + ], + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -44,7 +47,7 @@ const responseOnPageWithEmptyTitle = { id: 'title', type: 'title', title: [], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -61,14 +64,16 @@ const responseOnDb = { title: { id: 'title', type: 'title', - title: [{ - type: 'text', - text: { - content: 'dummy page title', + title: [ + { + type: 'text', + text: { + content: 'dummy page title', + }, + plain_text: 'dummy page title', }, - plain_text: 'dummy page title', - }], - } + ], + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -86,7 +91,7 @@ const responseOnDbWithEmptyTitle = { id: 'title', type: 'title', title: [], - } + }, }, url: 'https://www.notion.so/dummy-page-id', } @@ -94,83 +99,67 @@ const responseOnDbWithEmptyTitle = { describe('page:update', () => { describe('with page_id on a page flags', () => { apiMock(responseOnPage) - .command([ - 'page:update', - '--no-truncate', - 'dummy-page-id' - ]) - .it('shows retrieve page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:update', '--no-truncate', 'dummy-page-id']) + .it('shows retrieve page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) describe('with --raw flags', () => { apiMock(responseOnPage) - .command([ - 'page:update', - 'dummy-page-id', - '--raw' - ]) - .exit(0) - .it('shows a page object', ctx => { - expect(ctx.stdout).to.contain("object\": \"page") - expect(ctx.stdout).to.contain("id\": \"dummy-page-id") - expect(ctx.stdout).to.contain("url\": \"https://www.notion.so/dummy-page-id") - }) + .command(['page:update', 'dummy-page-id', '--raw']) + .exit(0) + .it('shows a page object', (ctx) => { + expect(ctx.stdout).to.contain('object": "page') + expect(ctx.stdout).to.contain('id": "dummy-page-id') + expect(ctx.stdout).to.contain('url": "https://www.notion.so/dummy-page-id') + }) }) describe('response title is [', () => { apiMock(responseOnPageWithEmptyTitle) - .command([ - 'page:update', - '--no-truncate', - 'dummy-page-id' - ]) - .it('shows retrieve page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:update', '--no-truncate', 'dummy-page-id']) + .it('shows retrieve page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) }) }) describe('with page_id on a db flags', () => { apiMock(responseOnDb) - .command([ - 'page:update', - '--no-truncate', - 'dummy-page-id' - ]) - .it('shows retrieve page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:update', '--no-truncate', 'dummy-page-id']) + .it('shows retrieve page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) describe('with --raw flags', () => { apiMock(responseOnDb) - .command([ - 'page:update', - 'dummy-page-id', - '--raw' - ]) - .exit(0) - .it('shows a page object', ctx => { - expect(ctx.stdout).to.contain("object\": \"page") - expect(ctx.stdout).to.contain("id\": \"dummy-page-id") - expect(ctx.stdout).to.contain("url\": \"https://www.notion.so/dummy-page-id") - }) + .command(['page:update', 'dummy-page-id', '--raw']) + .exit(0) + .it('shows a page object', (ctx) => { + expect(ctx.stdout).to.contain('object": "page') + expect(ctx.stdout).to.contain('id": "dummy-page-id') + expect(ctx.stdout).to.contain('url": "https://www.notion.so/dummy-page-id') + }) }) describe('response title is []', () => { apiMock(responseOnDbWithEmptyTitle) - .command([ - 'page:update', - '--no-truncate', - 'dummy-page-id' - ]) - .it('shows retrieve page result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - }) + .command(['page:update', '--no-truncate', 'dummy-page-id']) + .it('shows retrieve page result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + }) }) }) }) diff --git a/test/commands/search.test.ts b/test/commands/search.test.ts index 233e713..c76171c 100644 --- a/test/commands/search.test.ts +++ b/test/commands/search.test.ts @@ -1,10 +1,9 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' const apiMock = (response: any) => { - return test.nock('https://api.notion.com', api => api - .post('/v1/search') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + return test + .nock('https://api.notion.com', (api) => api.post('/v1/search').reply(200, response)) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) } const response = { @@ -25,10 +24,10 @@ const response = { content: 'dummy page title', }, plain_text: 'dummy page title', - } - ] - } - } + }, + ], + }, + }, }, { object: 'database', @@ -41,14 +40,14 @@ const response = { content: 'dummy database title', }, plain_text: 'dummy database title', - } + }, ], - } + }, ], next_cursor: null, has_more: false, type: 'page_or_database', - page_or_database: {} + page_or_database: {}, } const titleEmptyResponse = { @@ -62,54 +61,62 @@ const titleEmptyResponse = { Name: { id: 'title', type: 'title', - title: [] - } - } + title: [], + }, + }, }, { object: 'database', id: 'dummy-database-id', url: 'https://www.notion.so/dummy-database-id', - title: [] - } + title: [], + }, ], next_cursor: null, has_more: false, type: 'page_or_database', - page_or_database: {} + page_or_database: {}, } describe('search', () => { describe('with no flags', () => { apiMock(response) - // Need --no-truncate flag to match expected stdout - .command(['search', '--no-truncate']) - .it('shows search result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - expect(ctx.stdout).to.match(/dummy database title.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/) - }) + // Need --no-truncate flag to match expected stdout + .command(['search', '--no-truncate']) + .it('shows search result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /dummy page title.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + expect(ctx.stdout).to.match( + /dummy database title.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/ + ) + }) }) describe('with --raw flags', () => { apiMock(response) - .command(['search', '--raw']) - .exit(0) - .it('shows search result row json', ctx => { - expect(ctx.stdout).to.contain("object\": \"list") - expect(ctx.stdout).to.contain("url\": \"https://www.notion.so/dummy-page-id") - expect(ctx.stdout).to.contain("url\": \"https://www.notion.so/dummy-database-id") - expect(ctx.stdout).to.contain("type\": \"page_or_database") - }) + .command(['search', '--raw']) + .exit(0) + .it('shows search result row json', (ctx) => { + expect(ctx.stdout).to.contain('object": "list') + expect(ctx.stdout).to.contain('url": "https://www.notion.so/dummy-page-id') + expect(ctx.stdout).to.contain('url": "https://www.notion.so/dummy-database-id') + expect(ctx.stdout).to.contain('type": "page_or_database') + }) }) describe('response title is []', () => { apiMock(titleEmptyResponse) - .command(['search', '--no-truncate']) - .it('shows search result table', ctx => { - expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) - expect(ctx.stdout).to.match(/Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/) - expect(ctx.stdout).to.match(/Untitled.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/) - }) + .command(['search', '--no-truncate']) + .it('shows search result table', (ctx) => { + expect(ctx.stdout).to.match(/Title.*Object.*Id.*Url/) + expect(ctx.stdout).to.match( + /Untitled.*page.*dummy-page-id.*https:\/\/www\.notion\.so\/dummy-page-id/ + ) + expect(ctx.stdout).to.match( + /Untitled.*database.*dummy-database-id.*https:\/\/www\.notion\.so\/dummy-database-id/ + ) + }) }) }) diff --git a/test/commands/user/list.test.ts b/test/commands/user/list.test.ts index ad8ec62..c5c174e 100644 --- a/test/commands/user/list.test.ts +++ b/test/commands/user/list.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' describe('user:list', () => { const response = { @@ -22,42 +22,41 @@ describe('user:list', () => { owner: { type: 'workspace', workspace: true, - } + }, }, name: 'dummy-bot-name', avatar_url: 'dummy-bot-avatar-url', - } + }, ], - next_cursor: "dummy-next-cursor", + next_cursor: 'dummy-next-cursor', has_more: false, type: 'user', user: {}, } - const apiMock = test.nock('https://api.notion.com', api => api - .get('/v1/users') - .reply(200, response) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + const apiMock = test + .nock('https://api.notion.com', (api) => api.get('/v1/users').reply(200, response)) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) describe('with no flags', () => { - apiMock - .command(['user:list', '--no-truncate']) - .it('shows user list table', ctx => { + apiMock.command(['user:list', '--no-truncate']).it('shows user list table', (ctx) => { expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) - expect(ctx.stdout).to.match(/dummy-user-id.*dummy-user-name.*user.*person.*dummy-user-avatar-url/) + expect(ctx.stdout).to.match( + /dummy-user-id.*dummy-user-name.*user.*person.*dummy-user-avatar-url/ + ) expect(ctx.stdout).to.match(/dummy-bot-id.*dummy-bot-name.*user.*bot.*dummy-bot-avatar-url/) }) }) describe('with --raw flags', () => { apiMock - .command(['user:list', '--raw']) - .exit(0) - .it('shows a user list objects', ctx => { - expect(ctx.stdout).to.contain("object\": \"list") - expect(ctx.stdout).to.contain("object\": \"user") - expect(ctx.stdout).to.contain("type\": \"person") - expect(ctx.stdout).to.contain("type\": \"bot") - }) + .command(['user:list', '--raw']) + .exit(0) + .it('shows a user list objects', (ctx) => { + expect(ctx.stdout).to.contain('object": "list') + expect(ctx.stdout).to.contain('object": "user') + expect(ctx.stdout).to.contain('type": "person') + expect(ctx.stdout).to.contain('type": "bot') + }) }) }) diff --git a/test/commands/user/retrieve.test.ts b/test/commands/user/retrieve.test.ts index 486bf50..6b51703 100644 --- a/test/commands/user/retrieve.test.ts +++ b/test/commands/user/retrieve.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' describe('user:retrieve', () => { const responsePerson = { @@ -20,61 +20,67 @@ describe('user:retrieve', () => { owner: { type: 'workspace', workspace: true, - } + }, }, name: 'dummy-bot-name', avatar_url: 'dummy-bot-avatar-url', } - const apiMockPerson = test.nock('https://api.notion.com', api => api - .get('/v1/users/dummy-user-id') - .reply(200, responsePerson) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + const apiMockPerson = test + .nock('https://api.notion.com', (api) => + api.get('/v1/users/dummy-user-id').reply(200, responsePerson) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) describe('return person response', () => { describe('with no flags', () => { apiMockPerson - .command(['user:retrieve', '--no-truncate', 'dummy-user-id']) - .it('shows retrieved user table', ctx => { - expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) - expect(ctx.stdout).to.match(/dummy-user-id.*dummy-user-name.*user.*person.*dummy-user-avatar-url/) - }) + .command(['user:retrieve', '--no-truncate', 'dummy-user-id']) + .it('shows retrieved user table', (ctx) => { + expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) + expect(ctx.stdout).to.match( + /dummy-user-id.*dummy-user-name.*user.*person.*dummy-user-avatar-url/ + ) + }) }) describe('with --raw flags', () => { apiMockPerson - .command(['user:retrieve', '--raw', 'dummy-user-id']) - .exit(0) - .it('shows a retrieved user objects', ctx => { - expect(ctx.stdout).to.contain("object\": \"user") - expect(ctx.stdout).to.contain("type\": \"person") - }) + .command(['user:retrieve', '--raw', 'dummy-user-id']) + .exit(0) + .it('shows a retrieved user objects', (ctx) => { + expect(ctx.stdout).to.contain('object": "user') + expect(ctx.stdout).to.contain('type": "person') + }) }) }) - const apiMockBot = test.nock('https://api.notion.com', api => api - .get('/v1/users/dummy-user-id') - .reply(200, responseBot) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + const apiMockBot = test + .nock('https://api.notion.com', (api) => + api.get('/v1/users/dummy-user-id').reply(200, responseBot) + ) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) describe('return bot response', () => { describe('with no flags', () => { apiMockBot - .command(['user:retrieve', '--no-truncate', 'dummy-user-id']) - .it('shows retrieved user table', ctx => { - expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) - expect(ctx.stdout).to.match(/dummy-bot-id.*dummy-bot-name.*user.*bot.*dummy-bot-avatar-url/) - }) + .command(['user:retrieve', '--no-truncate', 'dummy-user-id']) + .it('shows retrieved user table', (ctx) => { + expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) + expect(ctx.stdout).to.match( + /dummy-bot-id.*dummy-bot-name.*user.*bot.*dummy-bot-avatar-url/ + ) + }) }) describe('with --raw flags', () => { apiMockBot - .command(['user:retrieve', '--raw', 'dummy-user-id']) - .exit(0) - .it('shows a retrieved user objects', ctx => { - expect(ctx.stdout).to.contain("object\": \"user") - expect(ctx.stdout).to.contain("type\": \"bot") - }) + .command(['user:retrieve', '--raw', 'dummy-user-id']) + .exit(0) + .it('shows a retrieved user objects', (ctx) => { + expect(ctx.stdout).to.contain('object": "user') + expect(ctx.stdout).to.contain('type": "bot') + }) }) }) }) diff --git a/test/commands/user/retrieve/bot.test.ts b/test/commands/user/retrieve/bot.test.ts index 80ad17a..32b5187 100644 --- a/test/commands/user/retrieve/bot.test.ts +++ b/test/commands/user/retrieve/bot.test.ts @@ -1,4 +1,4 @@ -import {expect, test} from '@oclif/test' +import { expect, test } from '@oclif/test' describe('user:retrieve:bot', () => { const responseOwnerUser = { @@ -20,7 +20,7 @@ describe('user:retrieve:bot', () => { email: 'dummy-user-email', }, }, - } + }, }, } @@ -39,55 +39,55 @@ describe('user:retrieve:bot', () => { }, } - const apiMockUser = test.nock('https://api.notion.com', api => api - .get('/v1/users/me') - .reply(200, responseOwnerUser) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + const apiMockUser = test + .nock('https://api.notion.com', (api) => api.get('/v1/users/me').reply(200, responseOwnerUser)) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) describe('owner is user', () => { describe('with no flags', () => { apiMockUser - .command(['user:retrieve:bot', '--no-truncate']) - .it('shows retrieved bot table', ctx => { - expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) - expect(ctx.stdout).to.match(/dummy-bot-id.*dummy-bot-name.*user.*bot.*dummy-bot-avatar-url/) - }) + .command(['user:retrieve:bot', '--no-truncate']) + .it('shows retrieved bot table', (ctx) => { + expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) + expect(ctx.stdout).to.match( + /dummy-bot-id.*dummy-bot-name.*user.*bot.*dummy-bot-avatar-url/ + ) + }) }) describe('with --raw flags', () => { apiMockUser - .command(['user:retrieve:bot', '--raw']) - .exit(0) - .it('shows a retrieved bot objects', ctx => { - expect(ctx.stdout).to.contain("object\": \"user") - expect(ctx.stdout).to.contain("type\": \"bot") - }) + .command(['user:retrieve:bot', '--raw']) + .exit(0) + .it('shows a retrieved bot objects', (ctx) => { + expect(ctx.stdout).to.contain('object": "user') + expect(ctx.stdout).to.contain('type": "bot') + }) }) }) - const apiMockWs = test.nock('https://api.notion.com', api => api - .get('/v1/users/me') - .reply(200, responseOwnerWs) - ).stdout({print: process.env.TEST_DEBUG ? true : false}) + const apiMockWs = test + .nock('https://api.notion.com', (api) => api.get('/v1/users/me').reply(200, responseOwnerWs)) + .stdout({ print: process.env.TEST_DEBUG ? true : false }) describe('owner is workspace', () => { describe('with no flags', () => { apiMockWs - .command(['user:retrieve:bot', '--no-truncate']) - .it('shows retrieved bot table', ctx => { - expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) - expect(ctx.stdout).to.match(/dummy-bot-id.*dummy-bot-name.*user.*bot.*/) - }) + .command(['user:retrieve:bot', '--no-truncate']) + .it('shows retrieved bot table', (ctx) => { + expect(ctx.stdout).to.match(/Id.*Name.*Object.*Type.*person\/bot.*Avatar url/) + expect(ctx.stdout).to.match(/dummy-bot-id.*dummy-bot-name.*user.*bot.*/) + }) }) describe('with --raw flags', () => { apiMockWs - .command(['user:retrieve:bot', '--raw']) - .exit(0) - .it('shows a retrieved bot objects', ctx => { - expect(ctx.stdout).to.contain("object\": \"user") - expect(ctx.stdout).to.contain("type\": \"bot") - }) + .command(['user:retrieve:bot', '--raw']) + .exit(0) + .it('shows a retrieved bot objects', (ctx) => { + expect(ctx.stdout).to.contain('object": "user') + expect(ctx.stdout).to.contain('type": "bot') + }) }) }) }) diff --git a/test/helper.test.ts b/test/helper.test.ts index 9a78972..ed7070d 100644 --- a/test/helper.test.ts +++ b/test/helper.test.ts @@ -1,7 +1,5 @@ -import {expect, test} from '@oclif/test' -import { - BlockObjectResponse, -} from '@notionhq/client/build/src/api-endpoints' +import { expect, test } from '@oclif/test' +import { BlockObjectResponse } from '@notionhq/client/build/src/api-endpoints' import * as helper from '../src/helper' const response = (type: BlockObjectResponse['type']): any => { @@ -143,31 +141,30 @@ describe('getBlockPlainText', () => { }) describe('type has rich_text object', () => { const richTextIncludesObjects = [ - "bulleted_list_item", - "callout", - "code", - "heading_1", - "heading_2", - "heading_3", - "numbered_list_item", - "paragraph", - "quote", - "to_do", - "toggle", + 'bulleted_list_item', + 'callout', + 'code', + 'heading_1', + 'heading_2', + 'heading_3', + 'numbered_list_item', + 'paragraph', + 'quote', + 'to_do', + 'toggle', ] - richTextIncludesObjects.forEach(type => { + richTextIncludesObjects.forEach((type) => { const res = response(type as BlockObjectResponse['type']) res[type] = { rich_text: [ { type: 'text', plain_text: `${type} dummy text`, - } - ] + }, + ], } test.it(() => { - expect(helper.getBlockPlainText(res)) - .to.equal(res[type].rich_text[0].plain_text) + expect(helper.getBlockPlainText(res)).to.equal(res[type].rich_text[0].plain_text) }) }) })