From 8bffd9bd34fa75c9774bcb61f416b8dd3b63c506 Mon Sep 17 00:00:00 2001 From: Jenae Janzen Date: Fri, 10 Nov 2023 12:11:19 -0500 Subject: [PATCH 1/6] chore: delete other templates --- .../.netlify-function-template.mjs | 5 -- .../javascript/identity-signup/{{name}}.js | 29 -------- .../.netlify-function-template.mjs | 5 -- .../javascript/image-external/{{name}}.js | 12 ---- .../.netlify-function-template.mjs | 5 -- .../javascript/localized-content/{{name}}.js | 15 ---- .../.netlify-function-template.mjs | 5 -- .../javascript/sanity-create/package.json | 20 ------ .../javascript/sanity-create/{{name}}.js | 72 ------------------- .../.netlify-function-template.mjs | 5 -- .../javascript/sanity-groq/package.json | 21 ------ .../javascript/sanity-groq/{{name}}.js | 56 --------------- .../.netlify-function-template.mjs | 6 -- .../scheduled-function/package.json | 20 ------ .../javascript/scheduled-function/{{name}}.js | 12 ---- .../.netlify-function-template.mjs | 5 -- .../javascript/set-cookies/{{name}}.js | 28 -------- .../.netlify-function-template.mjs | 5 -- .../javascript/set-req-header/{{name}}.js | 3 - .../.netlify-function-template.mjs | 5 -- .../javascript/set-res-header/{{name}}.js | 5 -- .../.netlify-function-template.mjs | 5 -- .../submission-created/package.json | 19 ----- .../javascript/submission-created/{{name}}.js | 29 -------- .../.netlify-function-template.mjs | 5 -- .../javascript/transform-response/{{name}}.js | 12 ---- .../abtest/.netlify-function-template.mjs | 5 -- .../typescript/abtest/{{name}}.ts | 31 -------- .../.netlify-function-template.mjs | 5 -- .../typescript/geolocation/{{name}}.ts | 24 ------- .../typescript/hello-world/{{name}}.ts | 2 +- .../json/.netlify-function-template.mjs | 5 -- .../typescript/json/{{name}}.ts | 5 -- .../.netlify-function-template.mjs | 6 -- .../scheduled-function/package.json | 22 ------ .../typescript/scheduled-function/{{name}}.ts | 12 ---- .../.netlify-function-template.mjs | 5 -- .../typescript/set-cookies/{{name}}.ts | 29 -------- .../.netlify-function-template.mjs | 5 -- .../typescript/set-req-header/{{name}}.ts | 5 -- .../.netlify-function-template.mjs | 5 -- .../typescript/set-res-header/{{name}}.ts | 7 -- .../.netlify-function-template.mjs | 5 -- .../typescript/transform-response/{{name}}.ts | 13 ---- 44 files changed, 1 insertion(+), 599 deletions(-) delete mode 100644 src/functions-templates/javascript/identity-signup/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/identity-signup/{{name}}.js delete mode 100644 src/functions-templates/javascript/image-external/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/image-external/{{name}}.js delete mode 100644 src/functions-templates/javascript/localized-content/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/localized-content/{{name}}.js delete mode 100644 src/functions-templates/javascript/sanity-create/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/sanity-create/package.json delete mode 100644 src/functions-templates/javascript/sanity-create/{{name}}.js delete mode 100644 src/functions-templates/javascript/sanity-groq/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/sanity-groq/package.json delete mode 100644 src/functions-templates/javascript/sanity-groq/{{name}}.js delete mode 100644 src/functions-templates/javascript/scheduled-function/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/scheduled-function/package.json delete mode 100644 src/functions-templates/javascript/scheduled-function/{{name}}.js delete mode 100644 src/functions-templates/javascript/set-cookies/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/set-cookies/{{name}}.js delete mode 100644 src/functions-templates/javascript/set-req-header/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/set-req-header/{{name}}.js delete mode 100644 src/functions-templates/javascript/set-res-header/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/set-res-header/{{name}}.js delete mode 100644 src/functions-templates/javascript/submission-created/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/submission-created/package.json delete mode 100644 src/functions-templates/javascript/submission-created/{{name}}.js delete mode 100644 src/functions-templates/javascript/transform-response/.netlify-function-template.mjs delete mode 100644 src/functions-templates/javascript/transform-response/{{name}}.js delete mode 100644 src/functions-templates/typescript/abtest/.netlify-function-template.mjs delete mode 100644 src/functions-templates/typescript/abtest/{{name}}.ts delete mode 100644 src/functions-templates/typescript/geolocation/.netlify-function-template.mjs delete mode 100644 src/functions-templates/typescript/geolocation/{{name}}.ts delete mode 100644 src/functions-templates/typescript/json/.netlify-function-template.mjs delete mode 100644 src/functions-templates/typescript/json/{{name}}.ts delete mode 100644 src/functions-templates/typescript/scheduled-function/.netlify-function-template.mjs delete mode 100644 src/functions-templates/typescript/scheduled-function/package.json delete mode 100644 src/functions-templates/typescript/scheduled-function/{{name}}.ts delete mode 100644 src/functions-templates/typescript/set-cookies/.netlify-function-template.mjs delete mode 100644 src/functions-templates/typescript/set-cookies/{{name}}.ts delete mode 100644 src/functions-templates/typescript/set-req-header/.netlify-function-template.mjs delete mode 100644 src/functions-templates/typescript/set-req-header/{{name}}.ts delete mode 100644 src/functions-templates/typescript/set-res-header/.netlify-function-template.mjs delete mode 100644 src/functions-templates/typescript/set-res-header/{{name}}.ts delete mode 100644 src/functions-templates/typescript/transform-response/.netlify-function-template.mjs delete mode 100644 src/functions-templates/typescript/transform-response/{{name}}.ts diff --git a/src/functions-templates/javascript/identity-signup/.netlify-function-template.mjs b/src/functions-templates/javascript/identity-signup/.netlify-function-template.mjs deleted file mode 100644 index 4da0d668b80..00000000000 --- a/src/functions-templates/javascript/identity-signup/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'identity-signup', - description: 'Identity Signup: Triggered when a new Netlify Identity user confirms. Assigns roles and extra metadata', - functionType: 'serverless', -} diff --git a/src/functions-templates/javascript/identity-signup/{{name}}.js b/src/functions-templates/javascript/identity-signup/{{name}}.js deleted file mode 100644 index d807873caed..00000000000 --- a/src/functions-templates/javascript/identity-signup/{{name}}.js +++ /dev/null @@ -1,29 +0,0 @@ -// note - this function MUST be named `identity-signup` to work -// we do not yet offer local emulation of this functionality in Netlify Dev -// -// more: -// https://www.netlify.com/blog/2019/02/21/the-role-of-roles-and-how-to-set-them-in-netlify-identity/ -// https://docs.netlify.com/functions/functions-and-identity/ - -const handler = async function (event) { - const data = JSON.parse(event.body) - const { user } = data - - const responseBody = { - app_metadata: { - roles: user.email.split('@')[1] === 'trust-this-company.com' ? ['editor'] : ['visitor'], - my_user_info: 'this is some user info', - }, - user_metadata: { - // append current user metadata - ...user.user_metadata, - custom_data_from_function: 'hurray this is some extra metadata', - }, - } - return { - statusCode: 200, - body: JSON.stringify(responseBody), - } -} - -module.exports = { handler } diff --git a/src/functions-templates/javascript/image-external/.netlify-function-template.mjs b/src/functions-templates/javascript/image-external/.netlify-function-template.mjs deleted file mode 100644 index fe7d838ec31..00000000000 --- a/src/functions-templates/javascript/image-external/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'image-external', - description: 'Fetches and serves an image from an external site', - functionType: 'edge', -} diff --git a/src/functions-templates/javascript/image-external/{{name}}.js b/src/functions-templates/javascript/image-external/{{name}}.js deleted file mode 100644 index 2a8c5388c4d..00000000000 --- a/src/functions-templates/javascript/image-external/{{name}}.js +++ /dev/null @@ -1,12 +0,0 @@ -export default async (request, context) => { - // Return an internal image using context.rewrite() - // This image is stored in the /public directory of this project - - // return context.rewrite("/apple-touch-icon.png"); - - // OR - - // Use fetch() and return the image response - const kitten = await fetch('https://placekitten.com/g/300/300') - return kitten -} diff --git a/src/functions-templates/javascript/localized-content/.netlify-function-template.mjs b/src/functions-templates/javascript/localized-content/.netlify-function-template.mjs deleted file mode 100644 index 66b7c5fbe0a..00000000000 --- a/src/functions-templates/javascript/localized-content/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'localized-content', - description: 'Uses geolocation data to serve localized countent according to country code', - functionType: 'edge', -} diff --git a/src/functions-templates/javascript/localized-content/{{name}}.js b/src/functions-templates/javascript/localized-content/{{name}}.js deleted file mode 100644 index acd4eb3402e..00000000000 --- a/src/functions-templates/javascript/localized-content/{{name}}.js +++ /dev/null @@ -1,15 +0,0 @@ -export default async (request, context) => { - const translations = { - UNKNOWN: 'Hello!', - US: "Howdy y'all!", - GB: 'How do you do?', - AU: "G'day, mate!", - } - - const countryCode = context.geo?.country?.code || 'UNKNOWN' - const countryName = context.geo?.country?.name || 'somewhere in the world' - - return new Response(`Your personalized greeting for ${countryName} is: ${translations[countryCode]}`, { - headers: { 'content-type': 'text/html' }, - }) -} diff --git a/src/functions-templates/javascript/sanity-create/.netlify-function-template.mjs b/src/functions-templates/javascript/sanity-create/.netlify-function-template.mjs deleted file mode 100644 index ada348cb028..00000000000 --- a/src/functions-templates/javascript/sanity-create/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'sanity-create', - description: 'Create documents in Sanity.io', - functionType: 'serverless', -} diff --git a/src/functions-templates/javascript/sanity-create/package.json b/src/functions-templates/javascript/sanity-create/package.json deleted file mode 100644 index 21b951bae00..00000000000 --- a/src/functions-templates/javascript/sanity-create/package.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "sanity-create", - "version": "1.0.0", - "description": "netlify functions:create - Create documents in Sanity.io", - "main": "sanity-create.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [ - "netlify", - "serverless", - "js", - "sanity" - ], - "author": "Sanity.io", - "license": "MIT", - "dependencies": { - "@sanity/client": "^0.147.3" - } -} diff --git a/src/functions-templates/javascript/sanity-create/{{name}}.js b/src/functions-templates/javascript/sanity-create/{{name}}.js deleted file mode 100644 index 10671d427d6..00000000000 --- a/src/functions-templates/javascript/sanity-create/{{name}}.js +++ /dev/null @@ -1,72 +0,0 @@ -const process = require('process') - -const sanityClient = require('@sanity/client') - -// You will need to configure environment variables for Sanity.io project id, -// dataset name, and a token with write access. The variables are named -// -// SANITY_PROJECTID -// SANITY_DATASET -// SANITY_TOKEN -// -// Create a Sanity.io token at https://manage.sanity.io by selecting your -// project, going to Settings -> API and adding a new token with write access. -// -// Read more about configuring Netlify environment variables at -// https://docs.netlify.com/configure-builds/environment-variables/#declare-variables -const client = sanityClient({ - projectId: process.env.SANITY_PROJECTID, - dataset: process.env.SANITY_DATASET, - token: process.env.SANITY_TOKEN, - useCdn: false, -}) - -// A function for writing to a Sanity.io dataset with a write access token. -// -// In this example we accept POST requests with the following JSON body -// -// { -// "author": "A name", -// "message": "What I want to say" -// } -// -// Then we construct an object to save in Sanity.io and return the full saved -// object back to our caller -const handler = async (event) => { - if (!event.httpMethod === 'POST') { - return { - statusCode: 400, - body: 'unrecognized HTTP Method, only POST allowed', - } - } - - const payload = JSON.parse(event.body) - if (!payload.message) { - return { status: 400, body: "Missing 'message'" } - } - - const document = { - _type: 'comment', - // Some workflow state - status: 'waitingApproval', - author: payload.author || 'Anonymous', - message: payload.message, - } - - try { - const result = await client.create(document) - return { - statusCode: 200, - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(result), - } - } catch (error) { - return { - headers: { 'Content-Type': 'application/json' }, - statusCode: 500, - body: error.responseBody || JSON.stringify({ error: 'An error occurred' }), - } - } -} - -module.exports = { handler } diff --git a/src/functions-templates/javascript/sanity-groq/.netlify-function-template.mjs b/src/functions-templates/javascript/sanity-groq/.netlify-function-template.mjs deleted file mode 100644 index d0bba0316fa..00000000000 --- a/src/functions-templates/javascript/sanity-groq/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'sanity-groq', - description: 'Query a Sanity.io dataset with GROQ', - functionType: 'serverless', -} diff --git a/src/functions-templates/javascript/sanity-groq/package.json b/src/functions-templates/javascript/sanity-groq/package.json deleted file mode 100644 index c62c2e1bc27..00000000000 --- a/src/functions-templates/javascript/sanity-groq/package.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "sanity-groq", - "version": "1.0.0", - "description": "netlify functions:create - Query a Sanity.io dataset with GROQ", - "main": "sanity-groq.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [ - "netlify", - "serverless", - "js", - "sanity", - "GROQ" - ], - "author": "Sanity.io", - "license": "MIT", - "dependencies": { - "@sanity/client": "^0.147.3" - } -} diff --git a/src/functions-templates/javascript/sanity-groq/{{name}}.js b/src/functions-templates/javascript/sanity-groq/{{name}}.js deleted file mode 100644 index 221c6212800..00000000000 --- a/src/functions-templates/javascript/sanity-groq/{{name}}.js +++ /dev/null @@ -1,56 +0,0 @@ -const process = require('process') - -const sanityClient = require('@sanity/client') - -// You will need to configure environment variables for Sanity.io project id -// and dataset name. Optionally you may also configure a token, useful for -// reading private datasets or mutating data. The variables are named -// -// SANITY_PROJECTID -// SANITY_DATASET -// SANITY_TOKEN -// -// Read more about configuring environment variables at -// https://docs.netlify.com/configure-builds/environment-variables/#declare-variables -const client = sanityClient({ - projectId: process.env.SANITY_PROJECTID, - dataset: process.env.SANITY_DATASET, - token: process.env.SANITY_TOKEN, - // CDN will not be used if token is set - useCdn: true, -}) - -// A proxy for Sanity.io GROQ queries. -// -// Useful for querying private datasets with a token. Usually you will restrict -// access to this function with for example Netlify Identity. -// -// To explore what queries you can do with GROQ, check out the cheat sheet at -// https://www.sanity.io/docs/query-cheat-sheet -// -// Create Sanity.io tokens at https://manage.sanity.io -// -// Read more about restricting access to your Netlify functions at -// https://www.netlify.com/blog/2018/03/29/jamstack-architecture-on-netlify-how-identity-and-functions-work-together/#restricting-access -const handler = async (event) => { - const { query = '' } = event.queryStringParameters - // The rest of the query params are handled as parameters to the query - const params = { ...event.queryStringParameters, query: null } - - try { - const result = await client.fetch(query, params) - return { - statusCode: 200, - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(result), - } - } catch (error) { - return { - headers: { 'Content-Type': 'application/json' }, - statusCode: error.statusCode || 500, - body: error.responseBody || JSON.stringify({ error: 'Unknown error occurred' }), - } - } -} - -module.exports = { handler } diff --git a/src/functions-templates/javascript/scheduled-function/.netlify-function-template.mjs b/src/functions-templates/javascript/scheduled-function/.netlify-function-template.mjs deleted file mode 100644 index 749caf89406..00000000000 --- a/src/functions-templates/javascript/scheduled-function/.netlify-function-template.mjs +++ /dev/null @@ -1,6 +0,0 @@ -export default { - name: 'scheduled-function', - priority: 1, - description: 'Basic implementation of a scheduled function in JavaScript.', - functionType: 'serverless', -} diff --git a/src/functions-templates/javascript/scheduled-function/package.json b/src/functions-templates/javascript/scheduled-function/package.json deleted file mode 100644 index 3ea45a59ed4..00000000000 --- a/src/functions-templates/javascript/scheduled-function/package.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "{{name}}", - "version": "1.0.0", - "description": "netlify functions:create - scheduled function in JavaScript", - "main": "{{name}}.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [ - "netlify", - "serverless", - "javascript", - "schedule" - ], - "author": "Netlify", - "license": "MIT", - "dependencies": { - "@netlify/functions": "^1.6.0" - } -} diff --git a/src/functions-templates/javascript/scheduled-function/{{name}}.js b/src/functions-templates/javascript/scheduled-function/{{name}}.js deleted file mode 100644 index f7767bde845..00000000000 --- a/src/functions-templates/javascript/scheduled-function/{{name}}.js +++ /dev/null @@ -1,12 +0,0 @@ -const { schedule } = require('@netlify/functions') - -// To learn about scheduled functions and supported cron extensions, -// see: https://ntl.fyi/sched-func -module.exports.handler = schedule('* * * * *', async (event) => { - const eventBody = JSON.parse(event.body) - console.log(`Next function run at ${eventBody.next_run}.`) - - return { - statusCode: 200, - } -}) diff --git a/src/functions-templates/javascript/set-cookies/.netlify-function-template.mjs b/src/functions-templates/javascript/set-cookies/.netlify-function-template.mjs deleted file mode 100644 index 0531b202948..00000000000 --- a/src/functions-templates/javascript/set-cookies/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'set-cookies', - description: 'Create and manage HTTP cookies', - functionType: 'edge', -} diff --git a/src/functions-templates/javascript/set-cookies/{{name}}.js b/src/functions-templates/javascript/set-cookies/{{name}}.js deleted file mode 100644 index e13a6f50f4d..00000000000 --- a/src/functions-templates/javascript/set-cookies/{{name}}.js +++ /dev/null @@ -1,28 +0,0 @@ -export default async (request, context) => { - const url = new URL(request.url) - - switch (url.searchParams.get('action')) { - case 'set': - context.cookies.set({ - name: 'action', - value: 'hello', - }) - - return new Response('Cookie value has been set. Reload this page without the "action" parameter to see it.') - - case 'clear': - context.cookies.delete('action') - - return new Response( - 'Cookie value has been cleared. Reload this page without the "action" parameter to see the new state.', - ) - default: - } - - const value = context.cookies.get('action') - const message = value - ? `Cookie value is "${value}". You can clear it by using "?action=clear".` - : 'Cookie has not been set. You can do so by adding "?action=set" to the URL.' - - return new Response(message) -} diff --git a/src/functions-templates/javascript/set-req-header/.netlify-function-template.mjs b/src/functions-templates/javascript/set-req-header/.netlify-function-template.mjs deleted file mode 100644 index 46b570e5573..00000000000 --- a/src/functions-templates/javascript/set-req-header/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'set-req-header', - description: 'Adds a custom HTTP header to HTTP request.', - functionType: 'edge', -} diff --git a/src/functions-templates/javascript/set-req-header/{{name}}.js b/src/functions-templates/javascript/set-req-header/{{name}}.js deleted file mode 100644 index c9325037ee9..00000000000 --- a/src/functions-templates/javascript/set-req-header/{{name}}.js +++ /dev/null @@ -1,3 +0,0 @@ -export default async (request, context) => { - request.headers.set('X-Your-Custom-Header', 'Your custom header value') -} diff --git a/src/functions-templates/javascript/set-res-header/.netlify-function-template.mjs b/src/functions-templates/javascript/set-res-header/.netlify-function-template.mjs deleted file mode 100644 index 7993e5621c8..00000000000 --- a/src/functions-templates/javascript/set-res-header/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'set-res-header', - description: 'Adds a custom HTTP header to HTTP response.', - functionType: 'edge', -} diff --git a/src/functions-templates/javascript/set-res-header/{{name}}.js b/src/functions-templates/javascript/set-res-header/{{name}}.js deleted file mode 100644 index eb2737915f5..00000000000 --- a/src/functions-templates/javascript/set-res-header/{{name}}.js +++ /dev/null @@ -1,5 +0,0 @@ -export default async (request, context) => { - const response = await context.next() - response.headers.set('X-Your-Custom-Header', 'A custom value') - return response -} diff --git a/src/functions-templates/javascript/submission-created/.netlify-function-template.mjs b/src/functions-templates/javascript/submission-created/.netlify-function-template.mjs deleted file mode 100644 index b48274513ff..00000000000 --- a/src/functions-templates/javascript/submission-created/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'submission-created', - description: 'submission-created: template for event triggered function when a new Netlify Form is submitted', - functionType: 'serverless', -} diff --git a/src/functions-templates/javascript/submission-created/package.json b/src/functions-templates/javascript/submission-created/package.json deleted file mode 100644 index fca1b5e2f1e..00000000000 --- a/src/functions-templates/javascript/submission-created/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "submission-created", - "version": "1.0.0", - "description": "netlify functions:create - template for submission-created event triggered function", - "main": "submission-created.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [ - "netlify", - "serverless", - "js" - ], - "author": "Netlify", - "license": "MIT", - "dependencies": { - "node-fetch": "^2.6.1" - } -} diff --git a/src/functions-templates/javascript/submission-created/{{name}}.js b/src/functions-templates/javascript/submission-created/{{name}}.js deleted file mode 100644 index 59dffe04063..00000000000 --- a/src/functions-templates/javascript/submission-created/{{name}}.js +++ /dev/null @@ -1,29 +0,0 @@ -// // optionally configure local env vars -// require('dotenv').config() - -// // details in https://css-tricks.com/using-netlify-forms-and-netlify-functions-to-build-an-email-sign-up-widget -const process = require('process') - -const fetch = require('node-fetch') - -const { EMAIL_TOKEN } = process.env -const handler = async (event) => { - const { email } = JSON.parse(event.body).payload - console.log(`Received a submission: ${email}`) - try { - const response = await fetch('https://api.buttondown.email/v1/subscribers', { - method: 'POST', - headers: { - Authorization: `Token ${EMAIL_TOKEN}`, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ email }), - }) - const data = await response.json() - console.log(`Submitted to Buttondown:\n ${data}`) - } catch (error) { - return { statusCode: 422, body: String(error) } - } -} - -module.exports = { handler } diff --git a/src/functions-templates/javascript/transform-response/.netlify-function-template.mjs b/src/functions-templates/javascript/transform-response/.netlify-function-template.mjs deleted file mode 100644 index 13057c1864f..00000000000 --- a/src/functions-templates/javascript/transform-response/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'transform-response', - description: 'Transform the content of an HTTP response', - functionType: 'edge', -} diff --git a/src/functions-templates/javascript/transform-response/{{name}}.js b/src/functions-templates/javascript/transform-response/{{name}}.js deleted file mode 100644 index 80ca82b9ec5..00000000000 --- a/src/functions-templates/javascript/transform-response/{{name}}.js +++ /dev/null @@ -1,12 +0,0 @@ -export default async (request, context) => { - const url = new URL(request.url) - - // Look for the query parameter, and return if we don't find it - if (url.searchParams.get('method') !== 'transform') { - return - } - - const response = await context.next() - const text = await response.text() - return new Response(text.toUpperCase(), response) -} diff --git a/src/functions-templates/typescript/abtest/.netlify-function-template.mjs b/src/functions-templates/typescript/abtest/.netlify-function-template.mjs deleted file mode 100644 index 2be0f0c198d..00000000000 --- a/src/functions-templates/typescript/abtest/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'abtest', - description: "Function that randomly assigns users to group 'a' or 'b' and sets this value as a cookie.", - functionType: 'edge', -} diff --git a/src/functions-templates/typescript/abtest/{{name}}.ts b/src/functions-templates/typescript/abtest/{{name}}.ts deleted file mode 100644 index 4544d2f1958..00000000000 --- a/src/functions-templates/typescript/abtest/{{name}}.ts +++ /dev/null @@ -1,31 +0,0 @@ -import type { Context } from "https://edge.netlify.com"; - -export default async (request: Request, context: Context) => { - // look for existing "test_bucket" cookie - const bucketName = "test_bucket"; - const bucket = context.cookies.get(bucketName); - - // return here if we find a cookie - if (bucket) { - return new Response(`Welcome back! You were assigned ${bucketName} **${bucket}** when you last visited the site!`); - } - - // if no "test_bucket" cookie is found, assign the user to a bucket - // in this example we're using two buckets (a, b) with an equal weighting of 50/50 - const weighting = 0.5; - - // get a random number between (0-1) - // this is a basic example and you may want to experiment - const random = Math.random(); - const newBucketValue = random <= weighting ? "a" : "b"; - - // set the new "test_bucket" cookie - context.cookies.set({ - name: bucketName, - value: newBucketValue, - }); - - return new Response( - `Congratulations! You have been assigned ${bucketName} **${newBucketValue}**. View your browser cookies to check it out!`, - ); -}; \ No newline at end of file diff --git a/src/functions-templates/typescript/geolocation/.netlify-function-template.mjs b/src/functions-templates/typescript/geolocation/.netlify-function-template.mjs deleted file mode 100644 index a48cdd3b468..00000000000 --- a/src/functions-templates/typescript/geolocation/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'geolocation', - description: "Returns info about user's geolocation, which can be used to serve location-specific content.", - functionType: 'edge', -} diff --git a/src/functions-templates/typescript/geolocation/{{name}}.ts b/src/functions-templates/typescript/geolocation/{{name}}.ts deleted file mode 100644 index 61377c58637..00000000000 --- a/src/functions-templates/typescript/geolocation/{{name}}.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Context } from "https://edge.netlify.com"; - -export default async (request: Request, context: Context) => { - // Here's what's available on context.geo - - // context: { - // geo: { - // city?: string; - // country?: { - // code?: string; - // name?: string; - // }, - // subdivision?: { - // code?: string; - // name?: string; - // }, - // } - // } - - return Response.json({ - geo: context.geo, - header: request.headers.get("x-nf-geo"), - }); -}; \ No newline at end of file diff --git a/src/functions-templates/typescript/hello-world/{{name}}.ts b/src/functions-templates/typescript/hello-world/{{name}}.ts index 79a2b3ab702..ae9b06b6d6d 100644 --- a/src/functions-templates/typescript/hello-world/{{name}}.ts +++ b/src/functions-templates/typescript/hello-world/{{name}}.ts @@ -1,4 +1,4 @@ -import { Handler } from '@netlify/functions' +import type { Context } from '@netlify/functions' export const handler: Handler = async (event, context) => { const { name = 'stranger' } = event.queryStringParameters diff --git a/src/functions-templates/typescript/json/.netlify-function-template.mjs b/src/functions-templates/typescript/json/.netlify-function-template.mjs deleted file mode 100644 index 2194476e3a6..00000000000 --- a/src/functions-templates/typescript/json/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'json', - description: 'Function that returns a simple json response', - functionType: 'edge', -} diff --git a/src/functions-templates/typescript/json/{{name}}.ts b/src/functions-templates/typescript/json/{{name}}.ts deleted file mode 100644 index 5df0da9e5d6..00000000000 --- a/src/functions-templates/typescript/json/{{name}}.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { Context } from "https://edge.netlify.com"; - -export default async (request: Request, context: Context) => { - return Response.json({ hello: "world", location: context.geo.city }); -}; \ No newline at end of file diff --git a/src/functions-templates/typescript/scheduled-function/.netlify-function-template.mjs b/src/functions-templates/typescript/scheduled-function/.netlify-function-template.mjs deleted file mode 100644 index dede74c13d6..00000000000 --- a/src/functions-templates/typescript/scheduled-function/.netlify-function-template.mjs +++ /dev/null @@ -1,6 +0,0 @@ -export default { - name: 'scheduled-function', - priority: 1, - description: 'Basic implementation of a scheduled function in TypeScript.', - functionType: 'serverless', -} diff --git a/src/functions-templates/typescript/scheduled-function/package.json b/src/functions-templates/typescript/scheduled-function/package.json deleted file mode 100644 index 433fa840f27..00000000000 --- a/src/functions-templates/typescript/scheduled-function/package.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "name": "{{name}}", - "version": "1.0.0", - "description": "netlify functions:create - scheduled function in TypeScript", - "main": "{{name}}.ts", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [ - "netlify", - "serverless", - "typescript", - "schedule" - ], - "author": "Netlify", - "license": "MIT", - "dependencies": { - "@netlify/functions": "^1.6.0", - "@types/node": "^18.0.0", - "typescript": "^4.5.5" - } -} diff --git a/src/functions-templates/typescript/scheduled-function/{{name}}.ts b/src/functions-templates/typescript/scheduled-function/{{name}}.ts deleted file mode 100644 index 40f8f56c563..00000000000 --- a/src/functions-templates/typescript/scheduled-function/{{name}}.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { schedule } from '@netlify/functions'; - -// To learn about scheduled functions and supported cron extensions, -// see: https://ntl.fyi/sched-func -export const handler = schedule("@hourly", async (event) => { - const eventBody = JSON.parse(event.body); - console.log(`Next function run at ${eventBody.next_run}.`); - - return { - statusCode: 200 - }; -}); diff --git a/src/functions-templates/typescript/set-cookies/.netlify-function-template.mjs b/src/functions-templates/typescript/set-cookies/.netlify-function-template.mjs deleted file mode 100644 index 0531b202948..00000000000 --- a/src/functions-templates/typescript/set-cookies/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'set-cookies', - description: 'Create and manage HTTP cookies', - functionType: 'edge', -} diff --git a/src/functions-templates/typescript/set-cookies/{{name}}.ts b/src/functions-templates/typescript/set-cookies/{{name}}.ts deleted file mode 100644 index 58749e33a50..00000000000 --- a/src/functions-templates/typescript/set-cookies/{{name}}.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { Context } from "https://edge.netlify.com"; - -export default async (request: Request, context: Context) => { - const url = new URL(request.url); - - switch (url.searchParams.get("action")) { - case "set": - context.cookies.set({ - name: "action", - value: "hello", - }); - - return new Response('Cookie value has been set. Reload this page without the "action" parameter to see it.'); - - case "clear": - context.cookies.delete("action"); - - return new Response( - 'Cookie value has been cleared. Reload this page without the "action" parameter to see the new state.', - ); - } - - const value = context.cookies.get("action"); - const message = value - ? `Cookie value is "${value}". You can clear it by using "?action=clear".` - : 'Cookie has not been set. You can do so by adding "?action=set" to the URL.'; - - return new Response(message); -}; \ No newline at end of file diff --git a/src/functions-templates/typescript/set-req-header/.netlify-function-template.mjs b/src/functions-templates/typescript/set-req-header/.netlify-function-template.mjs deleted file mode 100644 index 46b570e5573..00000000000 --- a/src/functions-templates/typescript/set-req-header/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'set-req-header', - description: 'Adds a custom HTTP header to HTTP request.', - functionType: 'edge', -} diff --git a/src/functions-templates/typescript/set-req-header/{{name}}.ts b/src/functions-templates/typescript/set-req-header/{{name}}.ts deleted file mode 100644 index 94bf8d6f253..00000000000 --- a/src/functions-templates/typescript/set-req-header/{{name}}.ts +++ /dev/null @@ -1,5 +0,0 @@ -import type { Context } from "https://edge.netlify.com"; - -export default async (request: Request, context: Context) => { - request.headers.set("X-Your-Custom-Header", "Your custom header value"); -}; \ No newline at end of file diff --git a/src/functions-templates/typescript/set-res-header/.netlify-function-template.mjs b/src/functions-templates/typescript/set-res-header/.netlify-function-template.mjs deleted file mode 100644 index 7993e5621c8..00000000000 --- a/src/functions-templates/typescript/set-res-header/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'set-res-header', - description: 'Adds a custom HTTP header to HTTP response.', - functionType: 'edge', -} diff --git a/src/functions-templates/typescript/set-res-header/{{name}}.ts b/src/functions-templates/typescript/set-res-header/{{name}}.ts deleted file mode 100644 index e734f1ed540..00000000000 --- a/src/functions-templates/typescript/set-res-header/{{name}}.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { Context } from "https://edge.netlify.com"; - -export default async (request: Request, context: Context) => { - const response = await context.next(); - response.headers.set("X-Your-Custom-Header", "A custom value"); - return response; -}; \ No newline at end of file diff --git a/src/functions-templates/typescript/transform-response/.netlify-function-template.mjs b/src/functions-templates/typescript/transform-response/.netlify-function-template.mjs deleted file mode 100644 index 13057c1864f..00000000000 --- a/src/functions-templates/typescript/transform-response/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'transform-response', - description: 'Transform the content of an HTTP response', - functionType: 'edge', -} diff --git a/src/functions-templates/typescript/transform-response/{{name}}.ts b/src/functions-templates/typescript/transform-response/{{name}}.ts deleted file mode 100644 index 2b897fd68c6..00000000000 --- a/src/functions-templates/typescript/transform-response/{{name}}.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { Context } from "https://edge.netlify.com"; - -export default async (request: Request, context: Context) => { - const url = new URL(request.url); - - // Look for the query parameter, and return if we don't find it - if (url.searchParams.get("method") !== "transform") { - return; - } - const response = await context.next(); - const text = await response.text(); - return new Response(text.toUpperCase(), response); -}; \ No newline at end of file From a3818ac0a658f3837f05421a0ed2d4376bd9788f Mon Sep 17 00:00:00 2001 From: Jenae Janzen Date: Fri, 10 Nov 2023 13:48:07 -0500 Subject: [PATCH 2/6] fix: only include hello world templates --- .../javascript/hello-world/{{name}}.js | 18 ++---------------- .../typescript/hello-world/{{name}}.ts | 13 +++---------- .../hello/.netlify-function-template.mjs | 5 +++++ .../typescript/{log => hello}/{{name}}.ts | 4 +--- .../log/.netlify-function-template.mjs | 5 ----- 5 files changed, 11 insertions(+), 34 deletions(-) create mode 100644 src/functions-templates/typescript/hello/.netlify-function-template.mjs rename src/functions-templates/typescript/{log => hello}/{{name}}.ts (55%) delete mode 100644 src/functions-templates/typescript/log/.netlify-function-template.mjs diff --git a/src/functions-templates/javascript/hello-world/{{name}}.js b/src/functions-templates/javascript/hello-world/{{name}}.js index fdf1fa5b1ae..5a742bfd65f 100644 --- a/src/functions-templates/javascript/hello-world/{{name}}.js +++ b/src/functions-templates/javascript/hello-world/{{name}}.js @@ -1,17 +1,3 @@ -// Docs on event and context https://docs.netlify.com/functions/build/#code-your-function-2 -const handler = async (event) => { - try { - const subject = event.queryStringParameters.name || 'World' - return { - statusCode: 200, - body: JSON.stringify({ message: `Hello ${subject}` }), - // // more keys you can return: - // headers: { "headerName": "headerValue", ... }, - // isBase64Encoded: true, - } - } catch (error) { - return { statusCode: 500, body: error.toString() } - } +export default async (req, context) => { + return new Response('Hello, world!') } - -module.exports = { handler } diff --git a/src/functions-templates/typescript/hello-world/{{name}}.ts b/src/functions-templates/typescript/hello-world/{{name}}.ts index ae9b06b6d6d..49dc7d8d8f1 100644 --- a/src/functions-templates/typescript/hello-world/{{name}}.ts +++ b/src/functions-templates/typescript/hello-world/{{name}}.ts @@ -1,12 +1,5 @@ -import type { Context } from '@netlify/functions' +import type { Context } from "@netlify/functions" -export const handler: Handler = async (event, context) => { - const { name = 'stranger' } = event.queryStringParameters - - return { - statusCode: 200, - body: JSON.stringify({ - message: `Hello, ${name}!`, - }), - } +export default async (req: Request, context: Context) => { + return new Response("Hello, world!") } diff --git a/src/functions-templates/typescript/hello/.netlify-function-template.mjs b/src/functions-templates/typescript/hello/.netlify-function-template.mjs new file mode 100644 index 00000000000..a64407a58c3 --- /dev/null +++ b/src/functions-templates/typescript/hello/.netlify-function-template.mjs @@ -0,0 +1,5 @@ +export default { + name: 'hello', + description: 'Basic function that shows async/await usage, and response formatting', + functionType: 'edge', +} diff --git a/src/functions-templates/typescript/log/{{name}}.ts b/src/functions-templates/typescript/hello/{{name}}.ts similarity index 55% rename from src/functions-templates/typescript/log/{{name}}.ts rename to src/functions-templates/typescript/hello/{{name}}.ts index 57fa4dd2c23..059ed923e0d 100644 --- a/src/functions-templates/typescript/log/{{name}}.ts +++ b/src/functions-templates/typescript/hello/{{name}}.ts @@ -1,9 +1,7 @@ import type { Context } from "https://edge.netlify.com"; export default async (request: Request, context: Context) => { - console.log(`There was a request from ${context.geo.city} to ${request.url}`); - - return new Response("The request to this URL was logged", { + return new Response("Hello, World!", { headers: { "content-type": "text/html" }, }); }; diff --git a/src/functions-templates/typescript/log/.netlify-function-template.mjs b/src/functions-templates/typescript/log/.netlify-function-template.mjs deleted file mode 100644 index 5d08eaf1f95..00000000000 --- a/src/functions-templates/typescript/log/.netlify-function-template.mjs +++ /dev/null @@ -1,5 +0,0 @@ -export default { - name: 'log', - description: 'Basic function logging context of request', - functionType: 'edge', -} From 4effc0868328ac92263eec307eec3f3feb4d2aa6 Mon Sep 17 00:00:00 2001 From: Jenae Janzen Date: Fri, 10 Nov 2023 14:59:14 -0500 Subject: [PATCH 3/6] fix: update test --- .../commands/functions-create/functions-create.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/commands/functions-create/functions-create.test.ts b/tests/integration/commands/functions-create/functions-create.test.ts index e75765fe2c9..dd8ad00062c 100644 --- a/tests/integration/commands/functions-create/functions-create.test.ts +++ b/tests/integration/commands/functions-create/functions-create.test.ts @@ -301,7 +301,7 @@ describe.concurrent('functions:create command', () => { const tomlContent = await readFile(toml, 'utf-8') expect(tomlContent.trim()).toMatchInlineSnapshot(` "[[edge_functions]] - function = \\"abtest\\" + function = \\"hello\\" path = \\"/test\\"" `) expect(existsSync(join(pkgBase, 'netlify/edge-functions/abtest/abtest.ts'))).toBe(true) From 34ef73a9c7afdddef93c0895f8da71516dda197b Mon Sep 17 00:00:00 2001 From: Jenae Janzen Date: Fri, 10 Nov 2023 15:15:58 -0500 Subject: [PATCH 4/6] fix: test --- .../commands/functions-create/functions-create.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/commands/functions-create/functions-create.test.ts b/tests/integration/commands/functions-create/functions-create.test.ts index dd8ad00062c..a85e200a58a 100644 --- a/tests/integration/commands/functions-create/functions-create.test.ts +++ b/tests/integration/commands/functions-create/functions-create.test.ts @@ -304,7 +304,7 @@ describe.concurrent('functions:create command', () => { function = \\"hello\\" path = \\"/test\\"" `) - expect(existsSync(join(pkgBase, 'netlify/edge-functions/abtest/abtest.ts'))).toBe(true) + expect(existsSync(join(pkgBase, 'netlify/edge-functions/hello/hello.ts'))).toBe(true) }) // we need to wait till file watchers are loaded // await pause(500) From fd1051089401875784080c5367643b9362d7c150 Mon Sep 17 00:00:00 2001 From: Jenae Janzen Date: Mon, 13 Nov 2023 11:37:20 -0500 Subject: [PATCH 5/6] fix: re-add set-res-header --- .../javascript/set-res-header/.netlify-function-template.mjs | 5 +++++ .../javascript/set-res-header/{{name}}.js | 5 +++++ 2 files changed, 10 insertions(+) create mode 100644 src/functions-templates/javascript/set-res-header/.netlify-function-template.mjs create mode 100644 src/functions-templates/javascript/set-res-header/{{name}}.js diff --git a/src/functions-templates/javascript/set-res-header/.netlify-function-template.mjs b/src/functions-templates/javascript/set-res-header/.netlify-function-template.mjs new file mode 100644 index 00000000000..7993e5621c8 --- /dev/null +++ b/src/functions-templates/javascript/set-res-header/.netlify-function-template.mjs @@ -0,0 +1,5 @@ +export default { + name: 'set-res-header', + description: 'Adds a custom HTTP header to HTTP response.', + functionType: 'edge', +} diff --git a/src/functions-templates/javascript/set-res-header/{{name}}.js b/src/functions-templates/javascript/set-res-header/{{name}}.js new file mode 100644 index 00000000000..eb2737915f5 --- /dev/null +++ b/src/functions-templates/javascript/set-res-header/{{name}}.js @@ -0,0 +1,5 @@ +export default async (request, context) => { + const response = await context.next() + response.headers.set('X-Your-Custom-Header', 'A custom value') + return response +} From b30cc8649de7f6396b49d5c8f6f788466fc6c1e3 Mon Sep 17 00:00:00 2001 From: Jenae Janzen Date: Mon, 13 Nov 2023 11:41:04 -0500 Subject: [PATCH 6/6] fix: re-add typescript version too --- .../set-res-header/.netlify-function-template.mjs | 5 +++++ .../typescript/set-res-header/{{name}}.ts | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 src/functions-templates/typescript/set-res-header/.netlify-function-template.mjs create mode 100644 src/functions-templates/typescript/set-res-header/{{name}}.ts diff --git a/src/functions-templates/typescript/set-res-header/.netlify-function-template.mjs b/src/functions-templates/typescript/set-res-header/.netlify-function-template.mjs new file mode 100644 index 00000000000..7993e5621c8 --- /dev/null +++ b/src/functions-templates/typescript/set-res-header/.netlify-function-template.mjs @@ -0,0 +1,5 @@ +export default { + name: 'set-res-header', + description: 'Adds a custom HTTP header to HTTP response.', + functionType: 'edge', +} diff --git a/src/functions-templates/typescript/set-res-header/{{name}}.ts b/src/functions-templates/typescript/set-res-header/{{name}}.ts new file mode 100644 index 00000000000..e734f1ed540 --- /dev/null +++ b/src/functions-templates/typescript/set-res-header/{{name}}.ts @@ -0,0 +1,7 @@ +import type { Context } from "https://edge.netlify.com"; + +export default async (request: Request, context: Context) => { + const response = await context.next(); + response.headers.set("X-Your-Custom-Header", "A custom value"); + return response; +}; \ No newline at end of file