netlify-cli 16.7.0 → 16.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -0
- package/npm-shrinkwrap.json +16123 -10639
- package/package.json +6 -4
- package/src/commands/build/build.mjs +4 -2
- package/src/commands/deploy/deploy.mjs +11 -3
- package/src/commands/integration/deploy.mjs +397 -0
- package/src/commands/integration/index.mjs +25 -0
- package/src/commands/main.mjs +2 -0
- package/src/lib/build.mjs +3 -1
- package/src/lib/edge-functions/consts.mjs +1 -0
- package/src/lib/edge-functions/proxy.mjs +1 -2
- package/src/lib/edge-functions/registry.mjs +15 -19
- package/src/lib/functions/registry.mjs +31 -4
- package/src/lib/functions/server.mjs +15 -0
- package/src/functions-templates/javascript/apollo-graphql/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/apollo-graphql/package.json +0 -21
- package/src/functions-templates/javascript/apollo-graphql/{{name}}.js +0 -42
- package/src/functions-templates/javascript/apollo-graphql-rest/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/apollo-graphql-rest/package.json +0 -22
- package/src/functions-templates/javascript/apollo-graphql-rest/random-user.js +0 -23
- package/src/functions-templates/javascript/apollo-graphql-rest/{{name}}.js +0 -68
- package/src/functions-templates/javascript/auth-fetch/.netlify-function-template.mjs +0 -11
- package/src/functions-templates/javascript/auth-fetch/package-lock.json +0 -83
- package/src/functions-templates/javascript/auth-fetch/package.json +0 -21
- package/src/functions-templates/javascript/auth-fetch/{{name}}.js +0 -39
- package/src/functions-templates/javascript/create-user/.netlify-function-template.mjs +0 -11
- package/src/functions-templates/javascript/create-user/package.json +0 -21
- package/src/functions-templates/javascript/create-user/{{name}}.js +0 -36
- package/src/functions-templates/javascript/fauna-crud/.netlify-function-template.mjs +0 -17
- package/src/functions-templates/javascript/fauna-crud/create-schema.js +0 -36
- package/src/functions-templates/javascript/fauna-crud/create.js +0 -37
- package/src/functions-templates/javascript/fauna-crud/delete.js +0 -29
- package/src/functions-templates/javascript/fauna-crud/package.json +0 -20
- package/src/functions-templates/javascript/fauna-crud/read-all.js +0 -33
- package/src/functions-templates/javascript/fauna-crud/read.js +0 -30
- package/src/functions-templates/javascript/fauna-crud/update.js +0 -30
- package/src/functions-templates/javascript/fauna-crud/{{name}}.js +0 -62
- package/src/functions-templates/javascript/fauna-graphql/.netlify-function-template.mjs +0 -17
- package/src/functions-templates/javascript/fauna-graphql/package.json +0 -26
- package/src/functions-templates/javascript/fauna-graphql/schema.graphql +0 -8
- package/src/functions-templates/javascript/fauna-graphql/sync-schema.js +0 -38
- package/src/functions-templates/javascript/fauna-graphql/{{name}}.js +0 -46
- package/src/functions-templates/javascript/google-analytics/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/google-analytics/package-lock.json +0 -100
- package/src/functions-templates/javascript/google-analytics/package.json +0 -25
- package/src/functions-templates/javascript/google-analytics/{{name}}.js +0 -114
- package/src/functions-templates/javascript/graphql-gateway/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/graphql-gateway/example-sibling-function-graphql-1.js +0 -42
- package/src/functions-templates/javascript/graphql-gateway/example-sibling-function-graphql-2.js +0 -80
- package/src/functions-templates/javascript/graphql-gateway/package.json +0 -24
- package/src/functions-templates/javascript/graphql-gateway/{{name}}.js +0 -75
- package/src/functions-templates/javascript/hasura-event-triggered/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/hasura-event-triggered/package.json +0 -21
- package/src/functions-templates/javascript/hasura-event-triggered/{{name}}.js +0 -40
- package/src/functions-templates/javascript/node-fetch/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/node-fetch/package.json +0 -19
- package/src/functions-templates/javascript/node-fetch/{{name}}.js +0 -29
- package/src/functions-templates/javascript/oauth-passport/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/oauth-passport/package.json +0 -25
- package/src/functions-templates/javascript/oauth-passport/utils/auth.js +0 -65
- package/src/functions-templates/javascript/oauth-passport/utils/config.js +0 -24
- package/src/functions-templates/javascript/oauth-passport/{{name}}.js +0 -37
- package/src/functions-templates/javascript/protected-function/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/protected-function/{{name}}.js +0 -25
- package/src/functions-templates/javascript/send-email/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/send-email/package.json +0 -21
- package/src/functions-templates/javascript/send-email/validations.js +0 -38
- package/src/functions-templates/javascript/send-email/{{name}}.js +0 -68
- package/src/functions-templates/javascript/serverless-ssr/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/serverless-ssr/app/index.js +0 -116
- package/src/functions-templates/javascript/serverless-ssr/package.json +0 -24
- package/src/functions-templates/javascript/serverless-ssr/serverless-http.js +0 -15
- package/src/functions-templates/javascript/serverless-ssr/{{name}}.js +0 -15
- package/src/functions-templates/javascript/set-cookie/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/set-cookie/package.json +0 -19
- package/src/functions-templates/javascript/set-cookie/{{name}}.js +0 -44
- package/src/functions-templates/javascript/slack-rate-limit/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/slack-rate-limit/package.json +0 -20
- package/src/functions-templates/javascript/slack-rate-limit/{{name}}.js +0 -115
- package/src/functions-templates/javascript/stripe-charge/.netlify-function-template.mjs +0 -28
- package/src/functions-templates/javascript/stripe-charge/package-lock.json +0 -196
- package/src/functions-templates/javascript/stripe-charge/package.json +0 -21
- package/src/functions-templates/javascript/stripe-charge/{{name}}.js +0 -56
- package/src/functions-templates/javascript/stripe-subscription/.netlify-function-template.mjs +0 -28
- package/src/functions-templates/javascript/stripe-subscription/package-lock.json +0 -196
- package/src/functions-templates/javascript/stripe-subscription/package.json +0 -21
- package/src/functions-templates/javascript/stripe-subscription/{{name}}.js +0 -52
- package/src/functions-templates/javascript/token-hider/.netlify-function-template.mjs +0 -29
- package/src/functions-templates/javascript/token-hider/package-lock.json +0 -317
- package/src/functions-templates/javascript/token-hider/package.json +0 -21
- package/src/functions-templates/javascript/token-hider/{{name}}.js +0 -37
- package/src/functions-templates/javascript/url-shortener/.netlify-function-template.mjs +0 -29
- package/src/functions-templates/javascript/url-shortener/generate-route.js +0 -53
- package/src/functions-templates/javascript/url-shortener/get-route.js +0 -32
- package/src/functions-templates/javascript/url-shortener/package-lock.json +0 -126
- package/src/functions-templates/javascript/url-shortener/package.json +0 -22
- package/src/functions-templates/javascript/url-shortener/{{name}}.js +0 -30
- package/src/functions-templates/javascript/using-middleware/.netlify-function-template.mjs +0 -5
- package/src/functions-templates/javascript/using-middleware/package.json +0 -19
- package/src/functions-templates/javascript/using-middleware/{{name}}.js +0 -60
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk'
|
|
2
|
-
|
|
3
|
-
export default {
|
|
4
|
-
name: 'url-shortener',
|
|
5
|
-
description: 'URL Shortener: simple URL shortener with Netlify Forms!',
|
|
6
|
-
functionType: 'serverless',
|
|
7
|
-
async onComplete() {
|
|
8
|
-
console.log(`${chalk.yellow('url-shortener')} function created from template!`)
|
|
9
|
-
if (!process.env.ROUTES_FORM_ID || !process.env.API_AUTH) {
|
|
10
|
-
console.log(
|
|
11
|
-
`note this function requires ${chalk.yellow('ROUTES_FORM_ID')} and ${chalk.yellow(
|
|
12
|
-
'API_AUTH',
|
|
13
|
-
)} build environment variables set in your Netlify Site.`,
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
let siteData = { name: 'YOURSITENAMEHERE' }
|
|
17
|
-
try {
|
|
18
|
-
siteData = await this.netlify.api.getSite({
|
|
19
|
-
siteId: this.netlify.site.id,
|
|
20
|
-
})
|
|
21
|
-
} catch (e) {
|
|
22
|
-
// silent error, not important
|
|
23
|
-
}
|
|
24
|
-
console.log(
|
|
25
|
-
`Set them at: https://app.netlify.com/sites/${siteData.name}/settings/deploys#environment-variables (must have CD setup)`,
|
|
26
|
-
)
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
const process = require('process')
|
|
3
|
-
|
|
4
|
-
const fetch = require('fetch')
|
|
5
|
-
const FormData = require('form-data')
|
|
6
|
-
const Hashids = require('hashids/cjs')
|
|
7
|
-
|
|
8
|
-
const NUMBER_TO_CODE = 100
|
|
9
|
-
|
|
10
|
-
module.exports = async function handler(event) {
|
|
11
|
-
// Set the root URL according to the Netlify site we are within
|
|
12
|
-
const rootURL = `${process.env.URL}/`
|
|
13
|
-
|
|
14
|
-
// get the details of what we are creating
|
|
15
|
-
let destination = event.queryStringParameters.to
|
|
16
|
-
|
|
17
|
-
// generate a unique short code (stupidly for now)
|
|
18
|
-
const hash = new Hashids()
|
|
19
|
-
const number = Math.round(Date.now() / NUMBER_TO_CODE)
|
|
20
|
-
const code = hash.encode(number)
|
|
21
|
-
|
|
22
|
-
// ensure that a protocol was provided
|
|
23
|
-
if (!destination.includes('://')) {
|
|
24
|
-
destination = `http://${destination}`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// prepare a payload to post
|
|
28
|
-
const form = new FormData()
|
|
29
|
-
form.append('form-name', 'routes')
|
|
30
|
-
form.append('destination', destination)
|
|
31
|
-
form.append('code', code)
|
|
32
|
-
form.append('expires', '')
|
|
33
|
-
|
|
34
|
-
// post the new route to the Routes form
|
|
35
|
-
try {
|
|
36
|
-
await fetch(rootURL, { method: 'POST', body: form })
|
|
37
|
-
const url = `${rootURL}${code}`
|
|
38
|
-
console.log(`Route registered. Site deploying to include it. ${url}`)
|
|
39
|
-
// tell the user what their shortcode will be
|
|
40
|
-
return {
|
|
41
|
-
statusCode: 200,
|
|
42
|
-
headers: { 'Content-Type': 'application/json' },
|
|
43
|
-
body: JSON.stringify({ url }),
|
|
44
|
-
}
|
|
45
|
-
} catch (error) {
|
|
46
|
-
return { statusCode: 500, body: `Post to Routes stash failed: ${error.message}` }
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// ENHANCEMENT: check for uniqueness of shortcode
|
|
50
|
-
// ENHANCEMENT: let the user provide their own shortcode
|
|
51
|
-
// ENHANCEMENT: dont' duplicate existing routes, return the current one
|
|
52
|
-
// ENHANCEMENT: allow the user to specify how long the redirect should exist for
|
|
53
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
const process = require('process')
|
|
3
|
-
|
|
4
|
-
const fetch = require('fetch')
|
|
5
|
-
|
|
6
|
-
module.exports = async function handler(event) {
|
|
7
|
-
// which URL code are we trying to retrieve?
|
|
8
|
-
const { code } = event.queryStringParameters
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
const response = await fetch(
|
|
12
|
-
`https://api.netlify.com/api/v1/forms/${process.env.ROUTES_FORM_ID}/submissions/?access_token=${process.env.API_AUTH}`,
|
|
13
|
-
)
|
|
14
|
-
const body = await response.text()
|
|
15
|
-
|
|
16
|
-
if (response.statusCode !== 200) {
|
|
17
|
-
return { statusCode: 500, body }
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const {
|
|
21
|
-
data: { destination },
|
|
22
|
-
} = body.find(({ data }) => data.code === code)
|
|
23
|
-
console.log(`We searched for ${code} and we found ${destination}`)
|
|
24
|
-
return {
|
|
25
|
-
statusCode: 200,
|
|
26
|
-
headers: { 'Content-Type': 'application/json' },
|
|
27
|
-
body: JSON.stringify({ code, url: destination }),
|
|
28
|
-
}
|
|
29
|
-
} catch (error) {
|
|
30
|
-
return { statusCode: 500, body: error.message }
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "url-shortener",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"lockfileVersion": 2,
|
|
5
|
-
"requires": true,
|
|
6
|
-
"packages": {
|
|
7
|
-
"": {
|
|
8
|
-
"name": "url-shortener",
|
|
9
|
-
"version": "1.0.0",
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"dependencies": {
|
|
12
|
-
"form-data": "^4.0.0",
|
|
13
|
-
"hashids": "^2.0.0"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
"node_modules/asynckit": {
|
|
17
|
-
"version": "0.4.0",
|
|
18
|
-
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
|
19
|
-
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
|
|
20
|
-
},
|
|
21
|
-
"node_modules/combined-stream": {
|
|
22
|
-
"version": "1.0.8",
|
|
23
|
-
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
|
24
|
-
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
|
25
|
-
"dependencies": {
|
|
26
|
-
"delayed-stream": "~1.0.0"
|
|
27
|
-
},
|
|
28
|
-
"engines": {
|
|
29
|
-
"node": ">= 0.8"
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
"node_modules/delayed-stream": {
|
|
33
|
-
"version": "1.0.0",
|
|
34
|
-
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
|
35
|
-
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=",
|
|
36
|
-
"engines": {
|
|
37
|
-
"node": ">=0.4.0"
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
"node_modules/form-data": {
|
|
41
|
-
"version": "4.0.0",
|
|
42
|
-
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
|
|
43
|
-
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
|
|
44
|
-
"dependencies": {
|
|
45
|
-
"asynckit": "^0.4.0",
|
|
46
|
-
"combined-stream": "^1.0.8",
|
|
47
|
-
"mime-types": "^2.1.12"
|
|
48
|
-
},
|
|
49
|
-
"engines": {
|
|
50
|
-
"node": ">= 6"
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
"node_modules/hashids": {
|
|
54
|
-
"version": "2.2.11",
|
|
55
|
-
"resolved": "https://registry.npmjs.org/hashids/-/hashids-2.2.11.tgz",
|
|
56
|
-
"integrity": "sha512-e1rAPT5Wa9vf/Dq1eq8LFw5VH2dtKxRf6k9bCw0GLwKL/SRnUphIcqwJWUZ1ki18cdj03nCrpdlJOcyHILQk8Q=="
|
|
57
|
-
},
|
|
58
|
-
"node_modules/mime-db": {
|
|
59
|
-
"version": "1.51.0",
|
|
60
|
-
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.51.0.tgz",
|
|
61
|
-
"integrity": "sha512-5y8A56jg7XVQx2mbv1lu49NR4dokRnhZYTtL+KGfaa27uq4pSTXkwQkFJl4pkRMyNFz/EtYDSkiiEHx3F7UN6g==",
|
|
62
|
-
"engines": {
|
|
63
|
-
"node": ">= 0.6"
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
"node_modules/mime-types": {
|
|
67
|
-
"version": "2.1.34",
|
|
68
|
-
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.34.tgz",
|
|
69
|
-
"integrity": "sha512-6cP692WwGIs9XXdOO4++N+7qjqv0rqxxVvJ3VHPh/Sc9mVZcQP+ZGhkKiTvWMQRr2tbHkJP/Yn7Y0npb3ZBs4A==",
|
|
70
|
-
"dependencies": {
|
|
71
|
-
"mime-db": "1.51.0"
|
|
72
|
-
},
|
|
73
|
-
"engines": {
|
|
74
|
-
"node": ">= 0.6"
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
"dependencies": {
|
|
79
|
-
"asynckit": {
|
|
80
|
-
"version": "0.4.0",
|
|
81
|
-
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
|
82
|
-
"integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k="
|
|
83
|
-
},
|
|
84
|
-
"combined-stream": {
|
|
85
|
-
"version": "1.0.8",
|
|
86
|
-
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
|
87
|
-
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
|
88
|
-
"requires": {
|
|
89
|
-
"delayed-stream": "~1.0.0"
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
"delayed-stream": {
|
|
93
|
-
"version": "1.0.0",
|
|
94
|
-
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
|
95
|
-
"integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk="
|
|
96
|
-
},
|
|
97
|
-
"form-data": {
|
|
98
|
-
"version": "4.0.0",
|
|
99
|
-
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
|
|
100
|
-
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
|
|
101
|
-
"requires": {
|
|
102
|
-
"asynckit": "^0.4.0",
|
|
103
|
-
"combined-stream": "^1.0.8",
|
|
104
|
-
"mime-types": "^2.1.12"
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
"hashids": {
|
|
108
|
-
"version": "2.2.11",
|
|
109
|
-
"resolved": "https://registry.npmjs.org/hashids/-/hashids-2.2.11.tgz",
|
|
110
|
-
"integrity": "sha512-e1rAPT5Wa9vf/Dq1eq8LFw5VH2dtKxRf6k9bCw0GLwKL/SRnUphIcqwJWUZ1ki18cdj03nCrpdlJOcyHILQk8Q=="
|
|
111
|
-
},
|
|
112
|
-
"mime-db": {
|
|
113
|
-
"version": "1.51.0",
|
|
114
|
-
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.51.0.tgz",
|
|
115
|
-
"integrity": "sha512-5y8A56jg7XVQx2mbv1lu49NR4dokRnhZYTtL+KGfaa27uq4pSTXkwQkFJl4pkRMyNFz/EtYDSkiiEHx3F7UN6g=="
|
|
116
|
-
},
|
|
117
|
-
"mime-types": {
|
|
118
|
-
"version": "2.1.34",
|
|
119
|
-
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.34.tgz",
|
|
120
|
-
"integrity": "sha512-6cP692WwGIs9XXdOO4++N+7qjqv0rqxxVvJ3VHPh/Sc9mVZcQP+ZGhkKiTvWMQRr2tbHkJP/Yn7Y0npb3ZBs4A==",
|
|
121
|
-
"requires": {
|
|
122
|
-
"mime-db": "1.51.0"
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "url-shortener",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "url-shortener.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
|
-
"keywords": [
|
|
10
|
-
"netlify",
|
|
11
|
-
"serverless",
|
|
12
|
-
"apis",
|
|
13
|
-
"url",
|
|
14
|
-
"js"
|
|
15
|
-
],
|
|
16
|
-
"author": "Netlify",
|
|
17
|
-
"license": "MIT",
|
|
18
|
-
"dependencies": {
|
|
19
|
-
"form-data": "^4.0.0",
|
|
20
|
-
"hashids": "^2.0.0"
|
|
21
|
-
}
|
|
22
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
const generateRoute = require('./generate-route.js')
|
|
2
|
-
const getRoute = require('./get-route.js')
|
|
3
|
-
|
|
4
|
-
const handler = async (event) => {
|
|
5
|
-
const path = event.path.replace(/\.netlify\/functions\/[^/]+/, '')
|
|
6
|
-
const segments = path.split('/').filter(Boolean)
|
|
7
|
-
console.log('segments', segments)
|
|
8
|
-
|
|
9
|
-
switch (event.httpMethod) {
|
|
10
|
-
case 'GET':
|
|
11
|
-
// e.g. GET /.netlify/functions/url-shortener
|
|
12
|
-
return getRoute(event)
|
|
13
|
-
case 'POST':
|
|
14
|
-
// e.g. POST /.netlify/functions/url-shortener
|
|
15
|
-
return generateRoute(event)
|
|
16
|
-
case 'PUT':
|
|
17
|
-
// your code here
|
|
18
|
-
return
|
|
19
|
-
case 'DELETE':
|
|
20
|
-
// your code here
|
|
21
|
-
return
|
|
22
|
-
default:
|
|
23
|
-
return {
|
|
24
|
-
statusCode: 500,
|
|
25
|
-
error: `unrecognized HTTP Method ${event.httpMethod}, must be one of GET/POST/PUT/DELETE`,
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = { handler }
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "using-middleware",
|
|
3
|
-
"version": "1.0.0",
|
|
4
|
-
"description": "netlify functions:create - using middleware with your netlify function",
|
|
5
|
-
"main": "using-middleware.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
-
},
|
|
9
|
-
"keywords": [
|
|
10
|
-
"netlify",
|
|
11
|
-
"serverless",
|
|
12
|
-
"js"
|
|
13
|
-
],
|
|
14
|
-
"author": "Netlify",
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"dependencies": {
|
|
17
|
-
"middy": "^0.36.0"
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
const middy = require('middy')
|
|
2
|
-
const { httpErrorHandler, httpHeaderNormalizer, jsonBodyParser, validator } = require('middy/middlewares')
|
|
3
|
-
|
|
4
|
-
/* Normal lambda code */
|
|
5
|
-
const businessLogic = async (event) => {
|
|
6
|
-
// event.body has already been turned into an object by `jsonBodyParser` middleware
|
|
7
|
-
const { name } = event.body
|
|
8
|
-
return {
|
|
9
|
-
statusCode: 200,
|
|
10
|
-
body: JSON.stringify({
|
|
11
|
-
result: 'success',
|
|
12
|
-
message: `Hi ${name} ⊂◉‿◉つ`,
|
|
13
|
-
}),
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/* Export inputSchema & outputSchema for automatic documentation */
|
|
18
|
-
const schema = {
|
|
19
|
-
input: {
|
|
20
|
-
type: 'object',
|
|
21
|
-
properties: {
|
|
22
|
-
body: {
|
|
23
|
-
type: 'object',
|
|
24
|
-
required: ['name'],
|
|
25
|
-
properties: {
|
|
26
|
-
name: { type: 'string' },
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
required: ['body'],
|
|
31
|
-
},
|
|
32
|
-
output: {
|
|
33
|
-
type: 'object',
|
|
34
|
-
properties: {
|
|
35
|
-
body: {
|
|
36
|
-
type: 'string',
|
|
37
|
-
required: ['result', 'message'],
|
|
38
|
-
properties: {
|
|
39
|
-
result: { type: 'string' },
|
|
40
|
-
message: { type: 'string' },
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
required: ['body'],
|
|
45
|
-
},
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const handler = middy(businessLogic)
|
|
49
|
-
.use(httpHeaderNormalizer())
|
|
50
|
-
// parses the request body when it's a JSON and converts it to an object
|
|
51
|
-
.use(jsonBodyParser())
|
|
52
|
-
// validates the input
|
|
53
|
-
.use(validator({ inputSchema: schema.input }))
|
|
54
|
-
// handles common http errors and returns proper responses
|
|
55
|
-
.use(httpErrorHandler())
|
|
56
|
-
|
|
57
|
-
module.exports = {
|
|
58
|
-
schema,
|
|
59
|
-
handler,
|
|
60
|
-
}
|