Skip to content

Commit 931e915

Browse files
authored
Merge pull request #9 from typeoneerror/create-with-template
Create page with template
2 parents 0373430 + ef2e4d9 commit 931e915

File tree

8 files changed

+419
-120
lines changed

8 files changed

+419
-120
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Arguments:
3+
*
4+
* --data-source-id: ID of the data source to fetch
5+
*/
6+
7+
const { notion, yargs } = require('../../shared');
8+
const { log } = require('../../shared/utils');
9+
10+
const dataSourceId = 'acce37c4c4ee4b78aa786a944c2577cf';
11+
const argv = yargs.default({ dataSourceId }).argv;
12+
13+
(async () => {
14+
const ds = await notion.dataSources.retrieve({
15+
data_source_id: argv.dataSourceId,
16+
});
17+
18+
log(ds);
19+
})();

examples/databases/retrieve/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* Arguments:
33
*
4-
* --database-id: ID of the page to fetch
4+
* --database-id: ID of the database to fetch
55
*/
66

77
const { notion, yargs } = require('../../shared');
88
const { log } = require('../../shared/utils');
99

10-
const databaseId = '87a5721f46b146dca5b3bddf414e9f00';
10+
const databaseId = '1e7abab87ee8457799c1155cf69d502a';
1111
const argv = yargs.default({ databaseId }).argv;
1212

1313
(async () => {

examples/pages/create-from-template/index.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

examples/shared/notion-api.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
const { default: axios } = require('axios');
3+
const dotenv = require('dotenv');
4+
dotenv.config();
5+
6+
const NOTION_VERSION = '2025-09-03'
7+
8+
const NOTION_HEADERS = {
9+
Authorization: `Bearer ${process.env.NOTION_API_TOKEN}`,
10+
'Notion-Version': NOTION_VERSION,
11+
};
12+
13+
const JSON_HEADERS = {
14+
...NOTION_HEADERS,
15+
Accept: 'application/json',
16+
'Content-Type': 'application/json',
17+
};
18+
19+
const notionAPI = axios.create({
20+
baseURL: 'https://api.notion.com/v1',
21+
headers: NOTION_HEADERS,
22+
});
23+
24+
module.exports = notionAPI;

examples/templates/create-from.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { notion, yargs } = require('../shared');
2+
const { log } = require('../shared/utils');
3+
4+
const dataSourceId = 'acce37c4c4ee4b78aa786a944c2577cf';
5+
const template = 'default';
6+
const argv = yargs.default({ dataSourceId, template }).argv;
7+
8+
(async () => {
9+
// Existing endpoint POST /v1/pages
10+
// template[type]=none (default behavior) doesn't use a template
11+
// template[type]=default uses the default template
12+
// template[type]=template_id; template[template_id]=... uses the provided template id
13+
14+
const template = !['none', 'default'].includes(argv.template)
15+
? { type: 'template_id', template_id: argv.template }
16+
: { type: argv.template };
17+
18+
const params = {
19+
parent: {
20+
type: 'data_source_id',
21+
data_source_id: argv.dataSourceId,
22+
},
23+
properties: {
24+
Name: {
25+
title: [
26+
{
27+
text: {
28+
content: 'Example page',
29+
},
30+
},
31+
],
32+
},
33+
},
34+
template,
35+
};
36+
37+
const page = await notion.pages.create(params);
38+
39+
log(page);
40+
})();

examples/templates/list.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const { notion, yargs } = require('../shared');
2+
const { log } = require('../shared/utils');
3+
4+
const dataSourceId = 'acce37c4c4ee4b78aa786a944c2577cf';
5+
const argv = yargs.default({ dataSourceId }).argv;
6+
7+
(async () => {
8+
const { templates } = await notion.dataSources.listTemplates({
9+
data_source_id: argv.dataSourceId,
10+
});
11+
12+
log(templates);
13+
})();

0 commit comments

Comments
 (0)