Skip to content

Commit 40ae315

Browse files
committed
Added fetchData module
1 parent 63ae965 commit 40ae315

File tree

4 files changed

+51
-51
lines changed

4 files changed

+51
-51
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
"homepage": "https://github.com/Devorein/github-action-learn-section-notion#readme",
2424
"dependencies": {
2525
"@actions/core": "^1.2.7",
26-
"@nishans/endpoints": "^0.0.32",
27-
"@vercel/ncc": "^0.28.5",
28-
"typescript": "^4.2.4"
26+
"@nishans/endpoints": "^0.0.32"
2927
},
3028
"devDependencies": {
3129
"@nishans/types": "^0.0.35",
3230
"@types/jest": "^26.0.23",
3331
"@types/node": "^15.0.1",
3432
"jest": "^26.6.3",
3533
"prettier": "^2.2.1",
36-
"ts-jest": "^26.5.5"
34+
"ts-jest": "^26.5.5",
35+
"@vercel/ncc": "^0.28.5",
36+
"typescript": "^4.2.4"
3737
}
38-
}
38+
}

src/index.ts

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,30 @@
11
import * as core from '@actions/core';
22
import { NotionEndpoints } from '@nishans/endpoints';
3-
import { ICollection, ICollectionBlock } from '@nishans/types';
3+
import { ICollection, TCollectionBlock } from '@nishans/types';
44
import fs from 'fs';
55
import { checkForSections } from './utils/checkForSections';
66
import { commitFile } from './utils/commitFile';
77
import { constructCategoriesMap } from './utils/constructCategoriesMap';
88
import { constructNewContents } from './utils/constructNewContents';
9+
import { fetchData } from './utils/fetchData';
910
import { getSchemaEntries } from './utils/getSchemaEntries';
1011
import { modifyRows } from './utils/modifyRows';
1112

1213
async function main() {
1314
try {
14-
const databaseId = core.getInput('database_id');
1515
const NOTION_TOKEN_V2 = core.getInput('token_v2');
16+
const databaseId = core.getInput('database_id');
1617

17-
const collectionViewData = await NotionEndpoints.Queries.syncRecordValues(
18-
{
19-
requests: [
20-
{
21-
id: databaseId,
22-
table: 'block',
23-
version: -1
24-
}
25-
]
26-
},
27-
{
28-
token: NOTION_TOKEN_V2,
29-
user_id: ''
30-
}
18+
const collectionView = await fetchData<TCollectionBlock>(
19+
databaseId,
20+
'block'
3121
);
32-
3322
core.info('Fetched database');
3423

35-
const collectionView = collectionViewData.recordMap.block![databaseId]
36-
.value as ICollectionBlock;
37-
38-
// If a database with the passed id doesn't exist
39-
if (!collectionView) {
40-
return core.setFailed(
41-
`Either your NOTION_TOKEN_V2 has expired or a database with id:${databaseId} doesn't exist`
42-
);
43-
}
44-
4524
const collection_id = collectionView.collection_id;
46-
const collectionData = await NotionEndpoints.Queries.syncRecordValues(
47-
{
48-
requests: [
49-
{
50-
id: collection_id,
51-
table: 'collection',
52-
version: -1
53-
}
54-
]
55-
},
56-
{
57-
token: NOTION_TOKEN_V2,
58-
user_id: ''
59-
}
25+
const collection = await fetchData<ICollection>(
26+
collection_id,
27+
'collection'
6028
);
6129

6230
core.info('Fetched collection');
@@ -80,9 +48,6 @@ async function main() {
8048
);
8149

8250
core.info('Fetched rows');
83-
84-
const collection = collectionData.recordMap.collection![collection_id]
85-
.value as ICollection;
8651
const { schema } = collection;
8752
const [category_schema_entry, color_schema_entry] = getSchemaEntries(
8853
schema
@@ -124,11 +89,11 @@ async function main() {
12489
try {
12590
await commitFile();
12691
} catch (err) {
127-
return core.setFailed(err.message);
92+
core.setFailed(err.message);
12893
}
12994
}
13095
} catch (error) {
131-
return core.setFailed(error.message);
96+
core.setFailed(error.message);
13297
}
13398
}
13499

src/utils/fetchData.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as core from '@actions/core';
2+
import { NotionEndpoints } from '@nishans/endpoints';
3+
import { RecordMap, TData } from '@nishans/types';
4+
5+
export const fetchData = async <T extends TData>(
6+
id: string,
7+
table: keyof RecordMap
8+
) => {
9+
const NOTION_TOKEN_V2 = core.getInput('token_v2');
10+
const response = await NotionEndpoints.Queries.syncRecordValues(
11+
{
12+
requests: [
13+
{
14+
id,
15+
table,
16+
version: -1
17+
}
18+
]
19+
},
20+
{
21+
token: NOTION_TOKEN_V2,
22+
user_id: ''
23+
}
24+
);
25+
26+
const data = response.recordMap[table]![id].value as T;
27+
28+
if (!data) {
29+
core.setFailed(
30+
`Either your NOTION_TOKEN_V2 has expired or a ${table} with id:${id} doesn't exist`
31+
);
32+
}
33+
34+
return data;
35+
};

tests/utils/fetchData.test.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)