|
| 1 | +'use strict' |
| 2 | +const fp = require('fastify-plugin') |
| 3 | + |
| 4 | +async function apiLayer (fastify, options) { |
| 5 | + const apiKey = options.apilayer.key |
| 6 | + |
| 7 | + fastify.decorate('apiLayer', { |
| 8 | + async post22 (content, url) { |
| 9 | + /* const myHeaders = new Headers() |
| 10 | + myHeaders.append('apikey', '6EwQsWNDR4AoSvFCHAsFUhmxkuALN13F') |
| 11 | +*/ |
| 12 | + const raw = 'You have done excellent work, and well done.' |
| 13 | + |
| 14 | + const requestOptions = { |
| 15 | + method: 'POST', |
| 16 | + redirect: 'follow', |
| 17 | + headers: { |
| 18 | + 'content-type': 'text/plain', |
| 19 | + apikey: '6EwQsWNDR4AoSvFCHAsFUhmxkuALN13F' |
| 20 | + }, |
| 21 | + body: raw |
| 22 | + } |
| 23 | + const response = await fetch(url, requestOptions) |
| 24 | + if (!response.ok) { |
| 25 | + const message = `An error has occured: ${response.status}` |
| 26 | + throw new Error(message) |
| 27 | + } |
| 28 | + return await response.json() |
| 29 | + }, |
| 30 | + async post (content, url) { |
| 31 | + const options = { |
| 32 | + method: 'POST', |
| 33 | + redirect: 'follow', |
| 34 | + headers: { |
| 35 | + 'content-type': 'text/plain', |
| 36 | + apikey: apiKey |
| 37 | + }, |
| 38 | + body: content |
| 39 | + } |
| 40 | + |
| 41 | + const response = await fetch(url, options) |
| 42 | + if (!response.ok) { |
| 43 | + const message = `An error has occured: ${response.status}` |
| 44 | + throw new Error(message) |
| 45 | + } |
| 46 | + return await response.json() |
| 47 | + } |
| 48 | + |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +module.exports = fp(apiLayer) |
0 commit comments