[TASK] Initial commit: basic firebase setup and part 1 almost solved (of-by-one)
This commit is contained in:
31
functions/.eslintrc.js
Normal file
31
functions/.eslintrc.js
Normal file
@@ -0,0 +1,31 @@
|
||||
module.exports = {
|
||||
root: true,
|
||||
env: {
|
||||
es6: true,
|
||||
node: true,
|
||||
},
|
||||
extends: [
|
||||
"eslint:recommended",
|
||||
"plugin:import/errors",
|
||||
"plugin:import/warnings",
|
||||
"plugin:import/typescript",
|
||||
"google",
|
||||
"plugin:@typescript-eslint/recommended",
|
||||
],
|
||||
parser: "@typescript-eslint/parser",
|
||||
parserOptions: {
|
||||
project: ["tsconfig.json", "tsconfig.dev.json"],
|
||||
sourceType: "module",
|
||||
},
|
||||
ignorePatterns: [
|
||||
"/lib/**/*", // Ignore built files.
|
||||
],
|
||||
plugins: [
|
||||
"@typescript-eslint",
|
||||
"import",
|
||||
],
|
||||
rules: {
|
||||
"quotes": ["error", "double"],
|
||||
"import/no-unresolved": 0,
|
||||
},
|
||||
};
|
||||
9
functions/.gitignore
vendored
Normal file
9
functions/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
# Compiled JavaScript files
|
||||
lib/**/*.js
|
||||
lib/**/*.js.map
|
||||
|
||||
# TypeScript v1 declaration files
|
||||
typings/
|
||||
|
||||
# Node.js dependency directory
|
||||
node_modules/
|
||||
30
functions/package.json
Normal file
30
functions/package.json
Normal file
@@ -0,0 +1,30 @@
|
||||
{
|
||||
"name": "functions",
|
||||
"scripts": {
|
||||
"lint": "eslint --ext .js,.ts .",
|
||||
"build": "tsc",
|
||||
"serve": "npm run build && firebase emulators:start --only functions",
|
||||
"shell": "npm run build && firebase functions:shell",
|
||||
"start": "npm run shell",
|
||||
"deploy": "firebase deploy --only functions",
|
||||
"logs": "firebase functions:log"
|
||||
},
|
||||
"engines": {
|
||||
"node": "16"
|
||||
},
|
||||
"main": "lib/index.js",
|
||||
"dependencies": {
|
||||
"firebase-admin": "^9.8.0",
|
||||
"firebase-functions": "^3.14.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "^3.9.1",
|
||||
"@typescript-eslint/parser": "^3.8.0",
|
||||
"eslint": "^7.6.0",
|
||||
"eslint-config-google": "^0.14.0",
|
||||
"eslint-plugin-import": "^2.22.0",
|
||||
"firebase-functions-test": "^0.2.0",
|
||||
"typescript": "^3.8.0"
|
||||
},
|
||||
"private": true
|
||||
}
|
||||
13
functions/src/day1.ts
Normal file
13
functions/src/day1.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
class Day1 {
|
||||
static part1(input: string[]): number {
|
||||
|
||||
let res = 0;
|
||||
for (let i = 1; i < input.length; i++) {
|
||||
if (input[i] > input[i-1]) res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
export default Day1;
|
||||
35
functions/src/index.ts
Normal file
35
functions/src/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import * as functions from "firebase-functions";
|
||||
import Utils from "./utils";
|
||||
import Day1 from "./day1";
|
||||
import {Response} from "firebase-functions";
|
||||
|
||||
|
||||
// // Start writing Firebase Functions
|
||||
// // https://firebase.google.com/docs/functions/typescript
|
||||
//
|
||||
// export const helloWorld = functions.https.onRequest((request, response) => {
|
||||
// functions.logger.info("Hello logs!", {structuredData: true});
|
||||
// response.send("Hello from Firebase!");
|
||||
// });
|
||||
|
||||
interface DayResult {
|
||||
part1: any;
|
||||
part2: any;
|
||||
}
|
||||
|
||||
export const day = {
|
||||
1: functions.region('europe-west1').https.onRequest((request, response) => {
|
||||
|
||||
const input = Utils.parseInput(request);
|
||||
const part1 = Day1.part1(input);
|
||||
|
||||
sendResponse(response, part1, 0);
|
||||
|
||||
}),
|
||||
}
|
||||
|
||||
function sendResponse(response: Response, part1: any, part2: any) {
|
||||
|
||||
const res: DayResult = { part1, part2 };
|
||||
response.send(res);
|
||||
}
|
||||
19
functions/src/utils.ts
Normal file
19
functions/src/utils.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import {Request} from "firebase-functions/lib/common/providers/https";
|
||||
|
||||
class Utils {
|
||||
static parseInput(request: Request): string[] {
|
||||
|
||||
const body = request.body;
|
||||
if (typeof body === 'string') {
|
||||
return body.split('\n');
|
||||
} else if (body.constructor === Array) {
|
||||
return body;
|
||||
} else {
|
||||
throw Error("Invalid request");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default Utils;
|
||||
5
functions/tsconfig.dev.json
Normal file
5
functions/tsconfig.dev.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"include": [
|
||||
".eslintrc.js"
|
||||
]
|
||||
}
|
||||
15
functions/tsconfig.json
Normal file
15
functions/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"noImplicitReturns": true,
|
||||
"noUnusedLocals": true,
|
||||
"outDir": "lib",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"target": "es2017"
|
||||
},
|
||||
"compileOnSave": true,
|
||||
"include": [
|
||||
"src"
|
||||
]
|
||||
}
|
||||
2746
functions/yarn.lock
Normal file
2746
functions/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user