-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7d7179b
Showing
9 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-typescript", | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": true | ||
} | ||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"env": { | ||
"es2021": true, | ||
"node": true, | ||
"commonjs": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/recommended" | ||
], | ||
"parser": "@typescript-eslint/parser", | ||
"parserOptions": { | ||
"ecmaVersion": 12, | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"@typescript-eslint" | ||
], | ||
"rules": { | ||
"linebreak-style": 0, | ||
"quotes": [ | ||
"error", | ||
"double" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"no-empty": "error", | ||
"no-func-assign": "error", | ||
"no-case-declarations": "off", | ||
"no-unreachable": "error", | ||
"no-eval": "error", | ||
"no-global-assign": "error", | ||
"@typescript-eslint/no-explicit-any": ["off"], | ||
"indent": [ | ||
"error", | ||
2 | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
node_modules | ||
lambda | ||
intro.md | ||
# Local Netlify folder | ||
.netlify | ||
yarn.lock | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 🧟 Monster As A Service (MAAS) | ||
|
||
[![Netlify Status](https://api.netlify.com/api/v1/badges/08e9878b-10bb-4706-b47a-bcc84c930c6a/deploy-status)](https://app.netlify.com/sites/friendly-curie-b02f70/deploys) | ||
|
||
This is a microservice wrapping requests to [D&D 5e API](https://www.dnd5eapi.co). | ||
|
||
It will return a random monster at every request. | ||
|
||
## Goal | ||
|
||
This repository is an example use-case on how to use Netlify Functions with TypeScript. | ||
|
||
[Watch the video on Youtube](https://youtu.be/3-Ie6p5ySKQ) | ||
|
||
## Deploy your own instance | ||
|
||
[![Deploy MAAS to Netlify](https://www.netlify.com/img/deploy/button.svg)](https://app.netlify.com/start/deploy?repository=https://github.com/atilafassina/monster-as-service) | ||
|
||
## More instructions | ||
|
||
If you need help understanding the ins and outs of this code, please refer to the [video on ScopeLeak]() | ||
|
||
### Packages | ||
|
||
| Dependencies | Why | | ||
| ----------------------- | --------------------------------------------- | | ||
| Netlify Dev¹ | To run Netlify Functions locally | | ||
| Netlify Lambda | Adds build-step to Netlify Functions | | ||
| TypeScript | The compiler for TypeScript (`tsc`) | | ||
| node-fetch | Brings `window.fetch` api to NodeJS | | ||
| babel/preset-env | Tells Babel which JavaScript syntax to output | | ||
| babel/preset-typescript | Teach Babel to use TypeScript compiler | | ||
| types/aws-lambda | Request/Response types for AWS Lambdas² | | ||
| encoding | depency of `node-fetch`³ | | ||
|
||
**1:** Install it globally on your system | ||
|
||
**2:** Netlify Functions uses AWS Lambdas under the hood | ||
|
||
**3:** It should be a dependency of `node-fetch` but it’s not added in v2, so we add manually so our build doesn‘t break. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This file configures your Netlify deploy | ||
# Settings declared here override everything on Dashboard interface | ||
# https://docs.netlify.com/configure-builds/file-based-configuration/ | ||
[build] | ||
command = "yarn build" | ||
functions = "lambda" | ||
|
||
[[redirects]] | ||
from = "/" | ||
to = "/.netlify/functions/index" | ||
status = 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "gaebolg", | ||
"version": "6.1.0-alpha.nightly", | ||
"main": "index.js", | ||
"author": "sinkaroid", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/scathachgrip/gaebolg.git" | ||
}, | ||
"license": "MIT", | ||
"private": true, | ||
"scripts": { | ||
"build": "netlify-lambda build src", | ||
"ts-check": "tsc --noEmit --lib ES2015 ./src/*.ts", | ||
"lint": "npx eslint . --ext .ts", | ||
"lint:fix": "npx eslint . --fix" | ||
}, | ||
"prettier": { | ||
"semi": false, | ||
"singleQuote": true | ||
}, | ||
"dependencies": { | ||
"encoding": "^0.1.13", | ||
"node-fetch": "^2.6.1" | ||
}, | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.11.0", | ||
"@babel/preset-typescript": "^7.10.4", | ||
"@types/aws-lambda": "^8.10.59", | ||
"@types/node-fetch": "^2.6.2", | ||
"@typescript-eslint/eslint-plugin": "^5.57.0", | ||
"eslint": "^8.36.0", | ||
"netlify-lambda": "^2.0.1", | ||
"prettier": "^2.0.5", | ||
"typescript": "^3.9.7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface iParams { | ||
type: string; | ||
image: string; | ||
} | ||
|
||
export const valid_type: string[] = ["hentai", "porn", "nasuverse"]; | ||
export const valid_image_hentai: string[] = ["3some", "69", "anal", "ass", "bdsm", "blowjob", "boobjob", "boobs", "cum", "dick_ride", | ||
"facesit", "feet", "finger", "furry", "futa", "handjob", "hentai", "kuni", "neko", "pet", | ||
"pussy", "sex", "solo", "spank", "strip", "tentacle", "toys", "wank", "yaoi", "yuri"]; | ||
export const valid_image_porn: string[] = ["ass", "blowjob", "boobs", "cowgirl", "cum", "doggie", "pussy", "sex"]; | ||
export const valid_image_nasuverse: string[] = ["angry", "bonk", "cry", "happy", "hug", "kiss", "lol", "nom", "pat", "pout", "smug", "uwu", "wink"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { APIGatewayEvent } from "aws-lambda"; | ||
import fetch from "node-fetch"; | ||
import { successDelivered, errorNoParams, errorTagsParams } from "./utils/handler"; | ||
import { iParams, valid_type, | ||
valid_image_hentai, valid_image_porn, valid_image_nasuverse } from "./constant/data"; | ||
|
||
export async function handler( | ||
event: APIGatewayEvent) { | ||
|
||
const userAgent = event.multiValueHeaders["user-agent"] ? event.multiValueHeaders["user-agent"][0] : "no user agent"; | ||
const gateway = { specs: event.queryStringParameters as unknown as iParams }; | ||
|
||
if (!valid_type.includes(gateway.specs.type)) return errorNoParams(userAgent); | ||
|
||
else if (gateway.specs.type === "hentai" | ||
&& !valid_image_hentai.includes(gateway.specs.image)) return errorTagsParams("hentai"); | ||
|
||
else if (gateway.specs.type === "porn" | ||
&& !valid_image_porn.includes(gateway.specs.image)) return errorTagsParams("porn"); | ||
|
||
else if (gateway.specs.type === "nasuverse" | ||
&& !valid_image_nasuverse.includes(gateway.specs.image)) return errorTagsParams("nasuverse"); | ||
|
||
else { | ||
try { | ||
let baseUrl = "", image = ""; | ||
if (gateway.specs.type === "hentai") | ||
baseUrl = "https://melony.scathach.id", image = gateway.specs.image; | ||
else if (gateway.specs.type === "porn") | ||
baseUrl = "https://tristan.scathach.id", image = gateway.specs.image; | ||
else if (gateway.specs.type === "nasuverse") | ||
baseUrl = "https://emiya.scathach.id", image = gateway.specs.image; | ||
|
||
// else return invalidParams(gateway.specs.type); | ||
|
||
const response = await fetch(`${baseUrl}/${image}/hey.json`); | ||
const res = await response.json(); | ||
const randomData = res[Math.floor(Math.random() * res.length)]; | ||
return successDelivered(randomData, userAgent); | ||
} catch (error) { | ||
return { | ||
statusCode: 500, | ||
body: JSON.stringify({ | ||
success: false, | ||
message: `An error occured: ${error.message}`, | ||
user_agent: userAgent | ||
}), | ||
}; | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { valid_image_hentai, valid_image_porn, valid_image_nasuverse } from "../constant/data"; | ||
import { version, name } from "../../package.json"; | ||
|
||
|
||
export async function errorNoParams(ua: string) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ | ||
success: false, | ||
version: `${name} ${version}`, | ||
params: [ | ||
{ | ||
type: "hentai", | ||
image: valid_image_hentai | ||
}, | ||
{ | ||
type: "porn", | ||
image: valid_image_porn | ||
}, | ||
{ | ||
type: "nasuverse", | ||
image: valid_image_nasuverse | ||
} | ||
], | ||
message: "You didn't provide any parameters or it's invalid", | ||
message_again: "Type properties tells it all about nsfw returns or not", | ||
user_agent: ua | ||
}), | ||
}; | ||
} | ||
|
||
export async function invalidParams(genre: string) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ | ||
message: `No matching parameters for ${genre}` | ||
}), | ||
}; | ||
} | ||
|
||
export async function errorTagsParams(genre: string) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ | ||
message: `Invalid parameters for ${genre}` | ||
}), | ||
}; | ||
} | ||
|
||
export async function maybeError(genre: string) { | ||
return { | ||
statusCode: 400, | ||
body: JSON.stringify({ | ||
message: `This parameter is not valid for ${genre}` | ||
}), | ||
}; | ||
} | ||
|
||
export async function successDelivered(url: string, userAgent: string) { | ||
return { | ||
statusCode: 200, | ||
headers: { | ||
"Content-Type": "application/json" | ||
}, | ||
body: JSON.stringify({ | ||
success: true, | ||
url: url, | ||
user_agent: userAgent | ||
}) | ||
}; | ||
} |