generated from fauna-labs/fauna-labs
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
170 lines (147 loc) · 3.9 KB
/
index.js
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
// Copyright Fauna, Inc.
// SPDX-License-Identifier: MIT-0
import {Router, listen} from 'worktop';
import faunadb from 'faunadb';
import {getFaunaError} from './utils.js';
const router = new Router();
const domain = globalThis.FAUNA_DOMAIN || 'db.fauna.com';
const secret = globalThis.FAUNA_SECRET || '';
const faunaClient = new faunadb.Client({
secret: secret,
headers: { 'X-Fauna-Source': 'fauna-workers' },
domain: domain,
});
const { Call } = faunadb.query;
// This route is to test that your API was deployed and works
// independent of any Fauna settings. It is safe to remove.
router.add('GET', '/', async (_request, response) => {
response.send(200, 'Hello, Fauna Workers!');
});
// Sending an HTTP POST request to this endpoint creates a new document
// in the "Products" collection.
//
// Expected body:
// {
// "serialNumber": <string>,
// "title": <string>,
// "weightLbs": <number>
// }
//
// Expected response:
// {
// "productId": <string>
// }
router.add('POST', '/products', async (request, response) => {
try {
const {serialNumber, title, weightLbs} = await request.body();
const result = await faunaClient.query(
Call(
"CreateProduct",
[
serialNumber,
title,
weightLbs,
0
]
)
);
response.send(200, {
productId: result.ref.id
});
} catch (error) {
const faunaError = getFaunaError(error);
response.send(faunaError.status, faunaError);
}
});
// Sending an HTTP GET request to this endpoint retrieves the document
// from the "Products" collection with the ID specified in the URL.
//
// Expected response:
// {
// "id": <string>,
// "serialNumber": <string>,
// "title": <string>,
// "weightLbs": <number>,
// "quantity": <number>
// }
router.add('GET', '/products/:productId', async (request, response) => {
try {
const productId = request.params.productId;
const result = await faunaClient.query(
Call(
"GetProductByID",
productId
)
);
response.send(200, result);
} catch (error) {
const faunaError = getFaunaError(error);
response.send(faunaError.status, faunaError);
}
});
// Sending an HTTP PATCH request to this endpoint creates a new document
// in the "Products" collection.
//
// For details on why this endpoint uses PATCH and not PUT, please see:
// https://fauna.com/blog/getting-started-with-fauna-and-cloudflare-workers#updating-the-inventory-quantity
//
// Expected body:
// {
// "quantity": <number>
// }
//
// Expected response:
// {
// "id": <string>,
// "serialNumber": <string>,
// "title": <string>,
// "weightLbs": <number>,
// "quantity": <number>
// }
router.add('PATCH', '/products/:productId/add-quantity', async (request, response) => {
try {
const productId = request.params.productId;
const {quantity} = await request.body();
const result = await faunaClient.query(
Call(
"AddProductQuantity",
[
productId,
quantity
]
)
);
response.send(200, result);
} catch (error) {
const faunaError = getFaunaError(error);
response.send(faunaError.status, faunaError);
}
});
// Sending an HTTP DELETE request to this endpoint removes the document
// with the ID specified in the URL from the "Products" collection.
//
// Expected response:
// {
// "id": <string>,
// "serialNumber": <string>,
// "title": <string>,
// "weightLbs": <number>,
// "quantity": <number>
// }
router.add('DELETE', '/products/:productId', async (request, response) => {
try {
const productId = request.params.productId;
const result = await faunaClient.query(
Call(
"DeleteProduct",
productId
)
);
response.send(200, result);
} catch (error) {
const faunaError = getFaunaError(error);
response.send(faunaError.status, faunaError);
}
});
// Start the Worktop router.
listen(router.run);