API examples, using Airtable vendor
We wrote a helper
It basically reconciliates all data that belongs to a customer and resolved nested deps.
It also handles i18n fields and resolves the best value to use based on the wanted locale and available translations.
fetchCustomer
that handles a lot of things.It basically reconciliates all data that belongs to a customer and resolved nested deps.
It also handles i18n fields and resolves the best value to use based on the wanted locale and available translations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Fetches all Airtable tables and returns a consolidated Customer object with all relations resolved
// Relations are only resolved on the two first levels (to avoid circular dependencies)
const customer: AirtableRecord<Customer> = await fetchCustomer(bestCountryCodes);
// "customer" print:
{
"id": "reci9HYsoqd1xScsi",
"fields": {
"id": "reci9HYsoqd1xScsi",
"termsEN": "# Those are the terms for {customerLabel}.\n\nBut ... Who wanna get bothered with **terms**?\nNot me\nMe neither it's boring\n\n- List element\n- List element\n- List element\n",
"terms": "# Voici les conditions générales d'utilisation pour {customerLabel}.\n\nMais... Qui a envie de lire des **CGU** ?\n\n- List element\n- List element\n- List element\n",
"termsFR": "# Voici les conditions générales d'utilisation pour {customerLabel}.\n\nMais... Qui a envie de lire des **CGU** ?\n\n- List element\n- List element\n- List element\n",
"labelFR": "Client 1",
"label": "Client 1",
"users": [
"rec5Ren090l6vGJDf"
],
"previewBaseUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app",
"products": [
{
"id": "reck5THYBOd0ryd5Z",
"fields": {
"id": 1,
"titleEN": "Vista Al Valle Zapote Honey",
"title": "Vista Al Valle Zapote Honey",
"descriptionEN": "Plummy taste and syrupy texture\n",
"description": "Café au sirop\n",
"customer": {
"id": "reci9HYsoqd1xScsi",
"fields": {
"termsEN": "# Those are the terms for {customerLabel}.\n\nBut ... Who wanna get bothered with **terms**?\nNot me\nMe neither it's boring\n\n- List element\n- List element\n- List element\n",
"termsFR": "# Voici les conditions générales d'utilisation pour {customerLabel}.\n\nMais... Qui a envie de lire des **CGU** ?\n\n- List element\n- List element\n- List element\n",
"labelFR": "Client 1",
"users": [
"rec5Ren090l6vGJDf"
],
"previewBaseUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app",
"products": [
"reck5THYBOd0ryd5Z",
"recFSrY2znI6Z8Dbj"
],
"theme": [
"recrcZANU6L73OA9v"
],
"ref": "customer1",
"previewTermsUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app/terms?fullPagePreview=1&updatedAt=2020-06-14T11:38:58.000Z"
},
"createdTime": "2020-06-02T14:01:51.000Z",
"__typename": "Customer"
},
"price": 10,
"images": [
{
"id": "attlk6ONaDfaZbQTw",
"url": "https://dl.airtable.com/OeNybctMTBKLPkbntK8p_jftonpzlxgakoxo9plfq.jpg",
"filename": "jftonpzlxgakoxo9plfq.jpg",
"size": 34481,
"type": "image/jpeg",
"thumbnails": {
"small": {
"url": "https://dl.airtable.com/qVbclResT9magEgEnERA_jftonpzlxgakoxo9plfq.jpg",
"width": 56,
"height": 36
},
"large": {
"url": "https://dl.airtable.com/p8e2P2cTKq817hQdo0yw_jftonpzlxgakoxo9plfq.jpg",
"width": 256,
"height": 163
}
},
"__typename": "Asset"
}
],
"descriptionFR": "Café au sirop\n",
"status": "PUBLISHED",
"imagesTitle": "Vista Al Valle Zapote Honey",
"ref": "vista-al-valle-zapote-honey",
"previewUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app/quick-preview/preview-product?ref=vista-al-valle-zapote-honey&updatedAt=2020-06-11T14:15:08.000Z"
},
"createdTime": "2020-06-02T14:04:46.000Z",
"__typename": "Product"
},
{
"id": "recFSrY2znI6Z8Dbj",
"fields": {
"id": 2,
"titleEN": "Big City",
"title": "Big City",
"descriptionEN": "Some awesome **description 2**\n\nYou can even [use links][1]\n\n [1]: https://bluebottlecoffee.com/releases/costa-rica-vista-al-valle-honey\n",
"description": "Super longue **description**\n\nVous pouvez même [utiliser des liens][1]\n\n [1]: https://bluebottlecoffee.com/releases/costa-rica-vista-al-valle-honey\n",
"customer": {
"id": "reci9HYsoqd1xScsi",
"fields": {
"termsEN": "# Those are the terms for {customerLabel}.\n\nBut ... Who wanna get bothered with **terms**?\nNot me\nMe neither it's boring\n\n- List element\n- List element\n- List element\n",
"termsFR": "# Voici les conditions générales d'utilisation pour {customerLabel}.\n\nMais... Qui a envie de lire des **CGU** ?\n\n- List element\n- List element\n- List element\n",
"labelFR": "Client 1",
"users": [
"rec5Ren090l6vGJDf"
],
"previewBaseUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app",
"products": [
"reck5THYBOd0ryd5Z",
"recFSrY2znI6Z8Dbj"
],
"theme": [
"recrcZANU6L73OA9v"
],
"ref": "customer1",
"previewTermsUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app/terms?fullPagePreview=1&updatedAt=2020-06-14T11:38:58.000Z"
},
"createdTime": "2020-06-02T14:01:51.000Z",
"__typename": "Customer"
},
"price": 23,
"images": [
{
"id": "att6JU52f5PlMuiRu",
"url": "https://dl.airtable.com/Uvg7ldEEQpqKhR3NKTGt_348s.jpg",
"filename": "348s.jpg",
"size": 17866,
"type": "image/jpeg",
"thumbnails": {
"small": {
"url": "https://dl.airtable.com/8C4cVNCES89lt6PnFH5W_348s.jpg",
"width": 36,
"height": 36
},
"large": {
"url": "https://dl.airtable.com/TdSPVnVQISc0P0EdiiQw_348s.jpg",
"width": 256,
"height": 256
}
},
"__typename": "Asset"
}
],
"descriptionFR": "Super longue **description**\n\nVous pouvez même [utiliser des liens][1]\n\n [1]: https://bluebottlecoffee.com/releases/costa-rica-vista-al-valle-honey\n",
"status": "PUBLISHED",
"imagesTitle": "Big City",
"ref": "big-city",
"previewUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app/quick-preview/preview-product?ref=big-city&updatedAt=2020-06-14T11:42:38.000Z"
},
"createdTime": "2020-06-02T14:04:46.000Z",
"__typename": "Product"
}
],
"theme": {
"id": "recrcZANU6L73OA9v",
"fields": {
"id": 1,
"customer": {
"id": "reci9HYsoqd1xScsi",
"fields": {
"termsEN": "# Those are the terms for {customerLabel}.\n\nBut ... Who wanna get bothered with **terms**?\nNot me\nMe neither it's boring\n\n- List element\n- List element\n- List element\n",
"termsFR": "# Voici les conditions générales d'utilisation pour {customerLabel}.\n\nMais... Qui a envie de lire des **CGU** ?\n\n- List element\n- List element\n- List element\n",
"labelFR": "Client 1",
"users": [
"rec5Ren090l6vGJDf"
],
"previewBaseUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app",
"products": [
"reck5THYBOd0ryd5Z",
"recFSrY2znI6Z8Dbj"
],
"theme": [
"recrcZANU6L73OA9v"
],
"ref": "customer1",
"previewTermsUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app/terms?fullPagePreview=1&updatedAt=2020-06-14T11:38:58.000Z"
},
"createdTime": "2020-06-02T14:01:51.000Z",
"__typename": "Customer"
},
"primaryColor": "red",
"logo": {
"id": "attPS7KMD2GYafnlr",
"url": "https://dl.airtable.com/Qj697gmbRLff6kiHcF71_mark.png",
"filename": "mark.png",
"size": 4904,
"type": "image/png",
"thumbnails": {
"small": {
"url": "https://dl.airtable.com/v51rtFINSuuxo3MwXvsG_small_mark.png",
"width": 36,
"height": 36
},
"large": {
"url": "https://dl.airtable.com/oqRnvriRey2urg6KJYlA_large_mark.png",
"width": 191,
"height": 193
},
"full": {
"url": "https://dl.airtable.com/AfwEuXK5TZu7dCIZq3qf_full_mark.png",
"width": 191,
"height": 193
}
},
"__typename": "Asset"
},
"logoTitle": "Awesome-looking logo",
"previewUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app?fullPagePreview=1&updatedAt=2020-06-14T11:07:45.000Z"
},
"createdTime": "2020-06-02T14:06:19.000Z",
"__typename": "Theme"
},
"ref": "customer1",
"previewTermsUrl": "https://nrn-v2-mst-aptd-at-lcz-sty-c1.vercel.app/terms?fullPagePreview=1&updatedAt=2020-06-14T11:38:58.000Z"
},
"createdTime": "2020-06-02T14:01:51.000Z",
"__typename": "Customer"
}
The above code is what we actually use in getDefaultStaticProps
, to fetch data that are shared by all SSG pages using the default
layout.
We use a cache using disk storage during build time (for SSG pages), so that each page doesn't have to fetch the Airtable API.
This is very important when generating static pages, otherwise we end up making hundreds of API calls (3 calls per page, because we fetch 3 tables).
Storing cache in memory doesn't work, because each page generation is executed in different processes (or so) and don't share the same memory (RAM).
Creating files like this requires a writable disk, which is the case with Vercel.
This is very important when generating static pages, otherwise we end up making hundreds of API calls (3 calls per page, because we fetch 3 tables).
Storing cache in memory doesn't work, because each page generation is executed in different processes (or so) and don't share the same memory (RAM).
Creating files like this requires a writable disk, which is the case with Vercel.
1
2
3
4
5
6
7
const customerRef = process.env.NEXT_PUBLIC_CUSTOMER_REF;
const { records: airtableCustomers } = await hybridCache('CustomerTable', async () => await fetchAirtableTable<GenericListApiResponse<AirtableRecord<Customer>>>('Customer'), {
enabled: !!process.env.IS_SERVER_INITIAL_BUILD && process.env.NODE_ENV !== 'development',
storage: { type: 'disk', options: { filename: 'CustomerTable' } },
});
The above code is part of what we actually use in fetchCustomer
, to fetch data from all tables.