Skip to content

Commit

Permalink
feat: implement page create when passing predicate function
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Feb 14, 2024
1 parent 2b25029 commit 97b9f95
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typed-notion-client",
"version": "0.0.0-internal.1",
"version": "0.0.0-internal.2",
"description": "Type-safe Notion API client",
"main": "./dist/main.js",
"module": "./dist/main.mjs",
Expand Down
19 changes: 19 additions & 0 deletions src/notion/notion-database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,23 @@ describe('Test processQueryPredicate', () => {
},
});
});

// test('When predicate is function, with helper function but it does not calling prop predicate, should throw error', () => {
// expect(() =>
// new FakeNotionDatabase({
// ...sharedOptions,
// propTypes: {
// Test: 'date',
// },
// }).processQueryPredicate(() => ({
// filter: {
// property: 'Test',
// date: {
// is_empty: true,
// },
// },
// }))
// ).toThrowError(`No prop type provided, please define typeProps in the constructor before calling query`);

// });
});
14 changes: 12 additions & 2 deletions src/notion/notion-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ export class NotionPage<T extends Record<string, PageProperties['type']> = Recor
return funcOrArgs;
}
const injectProps: Record<string, unknown> = {};
throw new Error(`Not implemented`);
for (const propName of Object.keys(this.propTypes)) {
injectProps[propName] = {
params: (params: Record<string, unknown>) => ({
[propName]: params,
}),
};
}

if (Object.keys(injectProps).length === 0) {
throw new Error(`No prop type provided, please define typeProps in the constructor before calling query`);
}

// return funcOrArgs(injectProps as );
return funcOrArgs(injectProps as MapTypeCreatePageProperties<T>);
}
}
58 changes: 58 additions & 0 deletions src/notion/notion.page.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect, test, describe } from 'vitest';
import { PageProperties } from './types';
import { NotionPage } from './notion-page';

class FakeNotionPage<T extends Record<string, PageProperties['type']>> extends NotionPage<T> {
override processCreatePredidcate = super.processCreatePredidcate;
}

describe('Test processCreatePredidcate', () => {
test('When predicate is plain filter object', () => {
expect(
new FakeNotionPage({ notionClient: {} as any }).processCreatePredidcate({
properties: {
Test: {
date: {
start: '2022-01-01',
},
},
},
})
).toStrictEqual({
properties: {
Test: {
date: {
start: '2022-01-01',
},
},
},
});
});

test('When predicate is function, with helper function', () => {
expect(
new FakeNotionPage({
notionClient: {} as any,
propTypes: {
Test: 'date',
},
}).processCreatePredidcate(prop => ({
properties: {
...prop['Test'].params({
date: {
start: '2022-01-01',
},
}),
},
}))
).toStrictEqual({
properties: {
Test: {
date: {
start: '2022-01-01',
},
},
},
});
});
});

0 comments on commit 97b9f95

Please sign in to comment.