Free javascript Hosting


serverless.js

Uploaded on Nov 21 2021 20:52 by gall

const { FullHttpResponse } = require('@wix/serverless-api');
module.exports = (functionsBuilder) =>
functionsBuilder
.addWebFunction('GET', '/say-hi', async () => {
return `Hello! Today is ${new Date().toLocaleDateString("en-US")}`;
})
.addWebFunction('GET', '/webhooks', async (ctx, req) => {
const webhooks = ctx.cloudStore.collection('Webhooks').build();
const webhooksToRetry = [];
for await (const item of webhooks.getAll()) {
webhooksToRetry.push(item.value);
}
return new FullHttpResponse({
status: 200,
body: {
items: webhooksToRetry,
},
});
})
.addWebFunction('POST', '/addWebhooks', async (ctx, req) => {
const webhookJson = req.body

const webhook = {
slug: webhookJson.slug,
kafkaTopic: webhookJson.kafkaTopic,
kafkaEventType: webhookJson.kafkaEventType,
permission: webhookJson.permission,
description: webhookJson.description,
deprecated: webhookJson.deprecated,
deprecatedBy: webhookJson.deprecatedBy,
deprecationRemovalDate: webhookJson.deprecationRemovalDate,
webhookExample: webhookJson.webhookExample
}
const webhooks = ctx.cloudStore.collection('Webhooks').build();
await webhooks.set({
key: webhook.slug,
value: webhook
})

return new FullHttpResponse({
status: 201
});
})
.addWebFunction('DELETE', '/deleteWebhook', async (ctx, req) => {
const webhookJson = req.body

const webhooks = ctx.cloudStore.collection('Webhooks').build();
const webhooksToRetry = [];
for await (const item of webhooks.getAll()) {
webhooksToRetry.push(item.value);
}
//optional: add errors
// ctx.logger.info(`webhooksToRetry.length: ${webhooksToRetry.length}`)
//
// if(webhooksToRetry.length == 0){
// throw new HttpError({
// status: 404,
// message: 'no webhooks to delete, webhooks list is empty'
// })
// }

await webhooks.delete(webhookJson.slug)

return new FullHttpResponse({
status: 200
});
})
.addWebFunction('DELETE', '/deleteAll', async (ctx, req) => {
const webhooks = ctx.cloudStore.collection('Webhooks').build();
for await (const wh of webhooks.getAll()){
webhooks.delete(wh.key)
webhooks.delete(wh)
}

return new FullHttpResponse({
status: 200
});
})

Back to list