import fetch from "cross-fetch"; export class Fetch { static async getPage(pageInfo : NotionPageArg) : Promise { const id = pageInfo.id const authToken = pageInfo.authToken try { const pageResponse:{ status: number, statusText: string, json: Function } = await fetch(`https://api.notion.com/v1/pages/${id}`, { method: 'GET', headers: { 'Notion-Version': '2022-06-28', 'Authorization': `Bearer ${authToken}` } }) if (pageResponse.status === 200) { const page:NotionPageType = await pageResponse.json(); return { page } } else { throw { status: pageResponse['status'], statusText: pageResponse['statusText']} } } catch (e:any) { console.error(e) return { is_error: true, error_status: e['status'], error_message: e['statusText'] } } } static async getBlocks(blockInfo: NotionBlockArg) : Promise { const id = blockInfo.id const authToken = blockInfo.authToken try { const blockResponse:{ status: number, statusText: string, json: Function } = await fetch(`https://api.notion.com/v1/blocks/${id}`, { method: 'GET', headers: { 'Notion-Version': '2022-02-22', 'Authorization': `Bearer ${authToken}` } }) if (blockResponse.status === 200) { const block:NotionBlockType = await blockResponse.json(); return { block } } else { throw { status: blockResponse['status'], statusText: blockResponse['statusText']} } } catch (e: any) { console.error(e) return { is_error: true, error_status: e['status'], error_message: e['statusText'] } } } static async getChildBlocks(blockInfo: NotionBlockArg, parentBlockId:string, pageSize:number = 100, startCursor?:string) : Promise { const id = parentBlockId const authToken = blockInfo.authToken try { const pageResponse:{ status: number, statusText: string, json: Function } = await fetch(`https://api.notion.com/v1/blocks/${id}/children?page_size=${pageSize}${startCursor ? '&start_cursor=' + startCursor : ''}`, { method: 'GET', headers: { 'Notion-Version': '2022-06-28', 'Authorization': `Bearer ${authToken}` } }) if (pageResponse.status === 200) { const block:NotionChildrenBlockType = await pageResponse.json(); return { block } } else { throw { status: pageResponse['status'], statusText: pageResponse['statusText']} } } catch (e: any) { console.error(`https://api.notion.com/v1/blocks/${id}/children?page_size=${pageSize}${startCursor ? '&start_cursor=' + startCursor : ''}`, e) return { is_error: true, error_status: e['status'], error_message: e['statusText'] } } } }