-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
5,075 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { | ||
GetBlockResponse, | ||
GetPageResponse, | ||
ListBlockChildrenResponse, | ||
QueryDatabaseResponse, | ||
} from "@notionhq/client/build/src/api-endpoints"; | ||
import NotionAPIWrapper from "../NotionAPIWrapper"; | ||
import dataMockPath from "./helpers/dataMockPath"; | ||
import { mockDataExists } from "./helpers/mockDataExists"; | ||
import getPayload from "./helpers/getPayload"; | ||
import savePayload from "./helpers/savePayload"; | ||
|
||
export default class MockNotionAPI extends NotionAPIWrapper { | ||
async getBlocks( | ||
id: string, | ||
all?: boolean | ||
): Promise<ListBlockChildrenResponse> { | ||
if (mockDataExists("ListBlockChildrenResponse", id)) { | ||
console.info("block exists using mock", id); | ||
return getPayload(dataMockPath("ListBlockChildrenResponse", id)); | ||
} | ||
const blocks = await super.getBlocks(id, all); | ||
savePayload(dataMockPath("ListBlockChildrenResponse", id), blocks); | ||
return blocks; | ||
} | ||
|
||
async getPage(id: string): Promise<GetPageResponse | null> { | ||
if (mockDataExists("GetPageResponse", id)) { | ||
console.info("page exists using mock", id); | ||
return getPayload(dataMockPath("GetPageResponse", id)); | ||
} | ||
const page = await super.getPage(id); | ||
savePayload(dataMockPath("GetPageResponse", id), page); | ||
return page; | ||
} | ||
|
||
async getBlock(id: string): Promise<GetBlockResponse> { | ||
if (mockDataExists("GetBlockResponse", id)) { | ||
console.info("block exists using mock", id); | ||
return getPayload(dataMockPath("GetBlockResponse", id)); | ||
} | ||
const block = await super.getBlock(id); | ||
savePayload(dataMockPath("GetBlockResponse", id), block); | ||
return block; | ||
} | ||
|
||
async queryDatabase( | ||
id: string, | ||
all?: boolean | ||
): Promise<QueryDatabaseResponse> { | ||
if (mockDataExists("QueryDatabaseResponse", id)) { | ||
console.info("database exists using mock", id); | ||
return getPayload(dataMockPath("QueryDatabaseResponse", id)); | ||
} | ||
const query = await super.queryDatabase(id, all); | ||
savePayload(dataMockPath("QueryDatabaseResponse", id), query); | ||
return query; | ||
} | ||
// do we need to mock search? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type MockType = | ||
| "ListBlockChildrenResponse" | ||
| "GetPageResponse" | ||
| "GetDatabaseResponse" | ||
| "QueryDatabaseResponse" | ||
| "GetBlockResponse"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import path from "path"; | ||
import ensureExists from "./ensureExists"; | ||
import { MockType } from "./MockType"; | ||
|
||
export default function dataMockPath(type: MockType, id: string): string { | ||
const dir = path.join(__dirname, `../payloads/${type}`); | ||
ensureExists(dir); | ||
return path.join(dir, `${id}.json`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import fs from "fs"; | ||
|
||
export default function ensureExists(location: string) { | ||
if (!fs.existsSync(location)) { | ||
console.info("creating: " + location); | ||
fs.mkdirSync(location, { recursive: true }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import fs from "fs"; | ||
|
||
export default function getPayload(path: string): any { | ||
return JSON.parse(fs.readFileSync(path).toString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import fs from "fs"; | ||
|
||
import dataMockPath from "./dataMockPath"; | ||
import { MockType } from "./MockType"; | ||
|
||
export function mockDataExists(type: MockType, id: string) { | ||
const path = dataMockPath(type, id); | ||
return fs.existsSync(path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import fs from "fs"; | ||
|
||
export default function savePayload(location: string, payload: any) { | ||
fs.writeFileSync(location, JSON.stringify(payload, null, 4)); | ||
} |
55 changes: 55 additions & 0 deletions
55
server/lib/notion/_mock/payloads/GetPageResponse/07a7b319-1836-42b9-afec-dcc4c456f73d.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"object": "page", | ||
"id": "07a7b319-1836-42b9-afec-dcc4c456f73d", | ||
"created_time": "2021-05-16T13:57:00.000Z", | ||
"last_edited_time": "2022-04-02T14:32:00.000Z", | ||
"created_by": { | ||
"object": "user", | ||
"id": "1590db54-99fe-467c-a656-be319fe6ca8b" | ||
}, | ||
"last_edited_by": { | ||
"object": "user", | ||
"id": "1590db54-99fe-467c-a656-be319fe6ca8b" | ||
}, | ||
"cover": { | ||
"type": "external", | ||
"external": { | ||
"url": "https://images.unsplash.com/photo-1514910574201-c68f4e58cb96?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjYzOTIxfQ" | ||
} | ||
}, | ||
"icon": { | ||
"type": "emoji", | ||
"emoji": "🧦" | ||
}, | ||
"parent": { | ||
"type": "page_id", | ||
"page_id": "3ce6b147-ac8a-425f-836b-51cc21825b85" | ||
}, | ||
"archived": false, | ||
"properties": { | ||
"title": { | ||
"id": "title", | ||
"type": "title", | ||
"title": [ | ||
{ | ||
"type": "text", | ||
"text": { | ||
"content": "HTML test", | ||
"link": null | ||
}, | ||
"annotations": { | ||
"bold": false, | ||
"italic": false, | ||
"strikethrough": false, | ||
"underline": false, | ||
"code": false, | ||
"color": "default" | ||
}, | ||
"plain_text": "HTML test", | ||
"href": null | ||
} | ||
] | ||
} | ||
}, | ||
"url": "https://www.notion.so/HTML-test-07a7b319183642b9afecdcc4c456f73d" | ||
} |
57 changes: 57 additions & 0 deletions
57
server/lib/notion/_mock/payloads/GetPageResponse/3ce6b147-ac8a-425f-836b-51cc21825b85.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"object": "page", | ||
"id": "3ce6b147-ac8a-425f-836b-51cc21825b85", | ||
"created_time": "2021-05-15T15:08:00.000Z", | ||
"last_edited_time": "2022-04-02T10:29:00.000Z", | ||
"created_by": { | ||
"object": "user", | ||
"id": "1590db54-99fe-467c-a656-be319fe6ca8b" | ||
}, | ||
"last_edited_by": { | ||
"object": "user", | ||
"id": "1590db54-99fe-467c-a656-be319fe6ca8b" | ||
}, | ||
"cover": null, | ||
"icon": null, | ||
"parent": { | ||
"type": "database_id", | ||
"database_id": "0bda98d9-cfb7-4a5b-8b61-536f9a68d6ff" | ||
}, | ||
"archived": false, | ||
"properties": { | ||
"Tags": { | ||
"id": "ZVmg", | ||
"type": "multi_select", | ||
"multi_select": [] | ||
}, | ||
"Created": { | ||
"id": "~ySo", | ||
"type": "created_time", | ||
"created_time": "2021-05-15T15:08:00.000Z" | ||
}, | ||
"Name": { | ||
"id": "title", | ||
"type": "title", | ||
"title": [ | ||
{ | ||
"type": "text", | ||
"text": { | ||
"content": "Notion API Test Page", | ||
"link": null | ||
}, | ||
"annotations": { | ||
"bold": false, | ||
"italic": false, | ||
"strikethrough": false, | ||
"underline": false, | ||
"code": false, | ||
"color": "default" | ||
}, | ||
"plain_text": "Notion API Test Page", | ||
"href": null | ||
} | ||
] | ||
} | ||
}, | ||
"url": "https://www.notion.so/Notion-API-Test-Page-3ce6b147ac8a425f836b51cc21825b85" | ||
} |
57 changes: 57 additions & 0 deletions
57
server/lib/notion/_mock/payloads/GetPageResponse/3ce6b147ac8a425f836b51cc21825b85.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"object": "page", | ||
"id": "3ce6b147-ac8a-425f-836b-51cc21825b85", | ||
"created_time": "2021-05-15T15:08:00.000Z", | ||
"last_edited_time": "2022-04-02T10:29:00.000Z", | ||
"created_by": { | ||
"object": "user", | ||
"id": "1590db54-99fe-467c-a656-be319fe6ca8b" | ||
}, | ||
"last_edited_by": { | ||
"object": "user", | ||
"id": "1590db54-99fe-467c-a656-be319fe6ca8b" | ||
}, | ||
"cover": null, | ||
"icon": null, | ||
"parent": { | ||
"type": "database_id", | ||
"database_id": "0bda98d9-cfb7-4a5b-8b61-536f9a68d6ff" | ||
}, | ||
"archived": false, | ||
"properties": { | ||
"Tags": { | ||
"id": "ZVmg", | ||
"type": "multi_select", | ||
"multi_select": [] | ||
}, | ||
"Created": { | ||
"id": "~ySo", | ||
"type": "created_time", | ||
"created_time": "2021-05-15T15:08:00.000Z" | ||
}, | ||
"Name": { | ||
"id": "title", | ||
"type": "title", | ||
"title": [ | ||
{ | ||
"type": "text", | ||
"text": { | ||
"content": "Notion API Test Page", | ||
"link": null | ||
}, | ||
"annotations": { | ||
"bold": false, | ||
"italic": false, | ||
"strikethrough": false, | ||
"underline": false, | ||
"code": false, | ||
"color": "default" | ||
}, | ||
"plain_text": "Notion API Test Page", | ||
"href": null | ||
} | ||
] | ||
} | ||
}, | ||
"url": "https://www.notion.so/Notion-API-Test-Page-3ce6b147ac8a425f836b51cc21825b85" | ||
} |
Oops, something went wrong.