Skip to content

Commit

Permalink
cache and rate fix
Browse files Browse the repository at this point in the history
  • Loading branch information
janzheng committed May 5, 2022
1 parent 012b2c5 commit d52ae96
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 44 deletions.
90 changes: 54 additions & 36 deletions src/api/notion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import {
JSONData,
NotionUserType,
Expand All @@ -22,23 +23,34 @@ const loadPageChunkBody = {
verticalColumns: false,
};

let ctr=0

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

const fetchNotionData = async <T extends any>({
resource,
body,
notionToken,
}: INotionParams): Promise<T> => {
const res = await fetch(`${NOTION_API}/${resource}`, {
method: "POST",
headers: {
"content-type": "application/json",
...(notionToken && { cookie: `token_v2=${notionToken}` }),
},

body: JSON.stringify(body),
});
try {
console.log('------ fetching notion data?', resource, body, ctr++)
const res = await fetch(`${NOTION_API}/${resource}`, {
method: "POST",
headers: {
"content-type": "application/json",
...(notionToken && { cookie: `token_v2=${notionToken}` }),
},

let json = await res.json()
return json;
body: JSON.stringify(body),
});

let json = await res.json()
// await delay(1000);
return json;
} catch(e) {
console.error('fetchNotionData error:', e, e.message)
throw new Error('Failed to pull data from Notion')
}
};

export const fetchPageById = async (pageId: string, notionToken?: string) => {
Expand Down Expand Up @@ -102,25 +114,25 @@ export const fetchNotionUsers = async (
userIds: string[],
notionToken?: string
) => {
const users = await fetchNotionData<{ results: NotionUserType[] }>({
resource: "getRecordValues",
body: {
requests: userIds.map((id) => ({ id, table: "notion_user" })),
},
notionToken,
});
if (users && users.results) {
return users.results.map((u) => {
const user = {
id: u.value.id,
firstName: u.value.given_name,
lastLame: u.value.family_name,
fullName: u.value.given_name + " " + u.value.family_name,
profilePhoto: u.value.profile_photo,
};
return user;
});
}
// const users = await fetchNotionData<{ results: NotionUserType[] }>({
// resource: "getRecordValues",
// body: {
// requests: userIds.map((id) => ({ id, table: "notion_user" })),
// },
// notionToken,
// });
// if (users && users.results) {
// return users.results.map((u) => {
// const user = {
// id: u.value.id,
// firstName: u.value.given_name,
// lastLame: u.value.family_name,
// fullName: u.value.given_name + " " + u.value.family_name,
// profilePhoto: u.value.profile_photo,
// };
// return user;
// });
// }
return [];
};

Expand All @@ -131,12 +143,18 @@ export const fetchBlocks = async (
return await fetchNotionData<LoadPageChunkData>({
resource: "syncRecordValues",
body: {
recordVersionMap: {
block: blockList.reduce((obj, blockId) => {
obj[blockId] = -1;
return obj;
}, {} as { [key: string]: -1 }),
},
// recordVersionMap: {
// block: blockList.reduce((obj, blockId) => {
// obj[blockId] = -1;
// return obj;
// }, {} as { [key: string]: -1 }),
// },

requests: blockList.map((id) => ({
id,
table: "block",
version: -1,
})),
},
notionToken,
});
Expand Down
23 changes: 18 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {} from "@cloudflare/workers-types";
// import {} from "@cloudflare/workers-types";
import { Router, Method } from "tiny-request-router";

import { pageRoute } from "./routes/page";
Expand Down Expand Up @@ -48,9 +48,22 @@ router.get("*", async () =>
)
);

const cache = (caches as any).default;
const NOTION_API_TOKEN =
typeof NOTION_TOKEN !== "undefined" ? NOTION_TOKEN : undefined;



// const match = router.match('GET' as Method, '/foobar')
// if (match) {
// // Call the async function of that match
// const response = await match.handler()
// console.log(response) // => Response('Hello')
// }



//cf-only cache
const cache = undefined; // (caches as any).default;
const NOTION_API_TOKEN = process.env.NOTION_TOKEN // not implemented yet — use .env later
// typeof env.NOTION_TOKEN !== "undefined" ? NOTION_TOKEN : undefined;

const handleRequest = async (fetchEvent: FetchEvent): Promise<Response> => {
const request = fetchEvent.request;
Expand Down Expand Up @@ -83,7 +96,7 @@ const handleRequest = async (fetchEvent: FetchEvent): Promise<Response> => {
notionToken,
});

if (cacheKey) {
if (cache && cacheKey) {
await cache.put(cacheKey, res.clone());
}

Expand Down
4 changes: 2 additions & 2 deletions src/routes/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export async function collectionRoute(req: HandlerRequest) {
let _op = filter.operator // "string_contains" etc.
let _type = filter.value && filter.value.type // "exact"
let _text = filter.value && filter.value.value // text matching against; "filter text"
let column = tableProps.find(c=>c.property==property)
let column = tableProps.find((c: any)=>c.property==property)

switch (_op) {
case 'string_contains':
Expand Down Expand Up @@ -177,7 +177,7 @@ export async function collectionRoute(req: HandlerRequest) {
tableData.rows = tableData.rows.filter((row:any)=> row[column.name] && row[column.name].includes(_text))
break;
case 'enum_does_not_contain':
tableData.rows = tableData.rows.filter(row=> {
tableData.rows = tableData.rows.filter((row: any)=> {
return !row[column.name] || (!row[column.name].includes(_text))
})
break;
Expand Down
1 change: 0 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ module.exports = {
transpileOnly: true,
},
},
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
],
},
optimization: {
Expand Down
32 changes: 32 additions & 0 deletions webpack.config_old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require("path");

const mode = process.env.NODE_ENV || "production";

module.exports = {
output: {
filename: `worker.${mode}.js`,
path: path.join(__dirname, "dist"),
},
target: "webworker",
devtool: "source-map",
mode,
resolve: {
extensions: [".ts", ".js"],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader",
options: {
transpileOnly: true,
},
},
{ enforce: "pre", test: /\.js$/, loader: "source-map-loader" },
],
},
optimization: {
usedExports: true,
},
};

0 comments on commit d52ae96

Please sign in to comment.