Skip to content

Commit

Permalink
Merge pull request #214 from ap2020/feature/update-eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzacat83 committed Mar 23, 2021
2 parents ea07767 + 7f4a992 commit e87c19d
Show file tree
Hide file tree
Showing 47 changed files with 2,187 additions and 1,669 deletions.
36 changes: 29 additions & 7 deletions aws/.eslintrc
@@ -1,11 +1,33 @@
{
"extends": ["@pizzacat83/eslint-config/typescript"],
"parserOptions": {
"project": "./tsconfig.json"
"ecmaVersion": 2019
},
"env": {
"jest": true
},
"rules": {
}
"overrides": [
{
"files": "./**.js",
"extends": ["@pizzacat83/eslint-config/nodeapp"]
},
{
"files": "**.ts",
"extends": ["@pizzacat83/eslint-config/typescript-nodeapp"],
"parserOptions": {
"project": "./tsconfig.json"
},
"env": {
"jest": true
},
"rules": {
"import/no-unresolved": [
"error",
{
"ignore": [
// aws-lambda and @types/aws-lambda is totally different and we only use the latter.
// There's no problem not to have aws-lambda because we only use type information.
"aws-lambda"
]
}
]
}
}
]
}
56 changes: 28 additions & 28 deletions aws/jest.config.js
@@ -1,36 +1,36 @@
const ignorePatterns = [
'/node_modules/',
'<rootDir>/.webpack/',
'<rootDir>/.dynamodb/',
'<rootDir>/.vscode/',
'/coverage/',
'/node_modules/',
'<rootDir>/.webpack/',
'<rootDir>/.dynamodb/',
'<rootDir>/.vscode/',
'/coverage/',
];

const notTestPatterns = [
'<rootDir>/src/lib/envvar/test.ts',
'<rootDir>/src/lib/envvar/test.ts',
];

module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
testPathIgnorePatterns: [
...ignorePatterns,
...notTestPatterns,
],
collectCoverageFrom: [
'**/*.{js,ts}',
'!**/*.test.{js,ts}'
],
coveragePathIgnorePatterns: [
...ignorePatterns,
'<rootDir>/jest.config.js',
'<rootDir>/.env.js',
],
setupFiles: [
'./test-utils/setup.ts',
],
moduleNameMapper: {
"^@/(.+)": "<rootDir>/src/$1"
},
preset: 'ts-jest',
testEnvironment: 'node',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
testPathIgnorePatterns: [
...ignorePatterns,
...notTestPatterns,
],
collectCoverageFrom: [
'**/*.{js,ts}',
'!**/*.test.{js,ts}',
],
coveragePathIgnorePatterns: [
...ignorePatterns,
'<rootDir>/jest.config.js',
'<rootDir>/.env.js',
],
setupFiles: [
'./test-utils/setup.ts',
],
moduleNameMapper: {
'^@/(.+)': '<rootDir>/src/$1',
},
};
15 changes: 12 additions & 3 deletions aws/package.json
@@ -1,5 +1,5 @@
{
"name": "@ap2020/bot-azure",
"name": "@ap2020/bot-aws",
"version": "1.0.0",
"description": "bot for ap2020",
"scripts": {
Expand All @@ -12,6 +12,7 @@
"dependencies": {
"@slack/web-api": "^5.13.0",
"@types/common-tags": "^1.8.0",
"aws-sdk": "^2.784.0",
"axios": "~0.21.1",
"common-tags": "^1.8.0",
"luxon": "^1.25.0",
Expand All @@ -28,11 +29,19 @@
"@types/node": "^12.19.3",
"@types/serverless": "^1.78.6",
"@types/tsscmp": "^1.0.0",
"aws-sdk": "^2.784.0",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"change-case": "^4.1.1",
"copy-webpack-plugin": "^6.1.1",
"dynamodb-admin": "^4.0.0",
"eslint": "^6.8.0",
"eslint": "^7.22.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.22.0",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-unicorn": "^29.0.0",
"fork-ts-checker-webpack-plugin": "^3.0.1",
"ifdef-loader": "^2.1.5",
"jest": "^26.6.2",
Expand Down
7 changes: 4 additions & 3 deletions aws/src/lib/case-insensitive-map.ts
Expand Up @@ -3,7 +3,7 @@ export class CaseInsensitiveMap<Value> {

constructor(map: Map<string, Value>) {
this.loweredMap = new Map(
[...map.entries()].map(([key, value]) => [key.toLowerCase(), value])
[...map.entries()].map(([key, value]) => [key.toLowerCase(), value]),
);
}

Expand All @@ -15,11 +15,12 @@ export class CaseInsensitiveMap<Value> {
return this.loweredMap.has(key.toLowerCase());
}

set(key: string, value: Value) {
set(key: string, value: Value): void {
this.loweredMap.set(key.toLowerCase(), value);
}

static fromObject<Value>(obj: {[key: string]: Value}): CaseInsensitiveMap<Value> {
// eslint-disable-next-line @typescript-eslint/no-shadow
static fromObject<Value>(obj: { [key: string]: Value }): CaseInsensitiveMap<Value> {
return new CaseInsensitiveMap<Value>(new Map(Object.entries(obj)));
}
}
14 changes: 7 additions & 7 deletions aws/src/lib/dynamodb.ts
@@ -1,10 +1,10 @@
import AWS from "aws-sdk";
import AWS from 'aws-sdk';

export const db = new AWS.DynamoDB.DocumentClient(
process.env.STAGE === 'prod' ?
{} :
{
region: 'localhost',
endpoint: 'http://localhost:8000',
},
process.env.STAGE === 'prod' ?
{} :
{
region: 'localhost',
endpoint: 'http://localhost:8000',
},
);
24 changes: 12 additions & 12 deletions aws/src/lib/envvar/base.ts
@@ -1,21 +1,21 @@
// TODO: type keys

export const envVarKeys = [
"aws/account-id",
"user-agent",
"utokyo-proxy/host",
"utokyo-proxy/port",
"utokyo-proxy/username",
"utokyo-proxy/password",
"slack/signing-secret",
"slack/token/bot",
"slack/channel/notify-others",
"slack/channel/notify-temp",
"slack/user_id/bot",
'aws/account-id',
'user-agent',
'utokyo-proxy/host',
'utokyo-proxy/port',
'utokyo-proxy/username',
'utokyo-proxy/password',
'slack/signing-secret',
'slack/token/bot',
'slack/channel/notify-others',
'slack/channel/notify-temp',
'slack/user_id/bot',
] as const;

export type EnvVarKey = typeof envVarKeys[number];

export type EnvVar = {
get<Key extends EnvVarKey>(key: Key): Promise<string>;
get<Key extends EnvVarKey>(key: Key): Promise<string>;
};
28 changes: 14 additions & 14 deletions aws/src/lib/envvar/index.ts
@@ -1,4 +1,4 @@
import { EnvVar } from './base';
import type { EnvVar } from './base';
import { envvarProd } from './prod';
/// #if STAGE === 'local'
import { envvarTest } from './test';
Expand All @@ -11,18 +11,18 @@ import { envvarLocal } from './local';


export const envvar: EnvVar = (() => {
switch (process.env.STAGE) {
case 'prod':
case 'dev': {
return envvarProd;
}
/// #if STAGE === 'local'
case 'test': {
return envvarTest;
}
default: {
return envvarLocal;
}
/// #endif
switch (process.env.STAGE) {
case 'prod':
case 'dev': {
return envvarProd;
}
/// #if STAGE === 'local'
case 'test': {
return envvarTest;
}
default: {
return envvarLocal;
}
/// #endif
}
})();
12 changes: 6 additions & 6 deletions aws/src/lib/envvar/local.ts
@@ -1,12 +1,12 @@
import envvarObj from '@/../.env.local.json'
import { EnvVar, EnvVarKey } from './base';
import envvarObj from '@/../.env.local.json';
import type { EnvVar, EnvVarKey } from './base';

class EnvVarLocal implements EnvVar {
envvars: Map<EnvVarKey, string> = new Map(Object.entries(envvarObj).map(([k, v]) => [k as EnvVarKey, v.toString()]));
envvars: Map<EnvVarKey, string> = new Map(Object.entries(envvarObj).map(([k, v]) => [k as EnvVarKey, v.toString()]));

get<Key extends EnvVarKey>(key: Key) {
return Promise.resolve(this.envvars.get(key));
}
get<Key extends EnvVarKey>(key: Key) {
return Promise.resolve(this.envvars.get(key));
}
}

export const envvarLocal = new EnvVarLocal();
38 changes: 20 additions & 18 deletions aws/src/lib/envvar/prod.ts
@@ -1,28 +1,30 @@
import AWS from 'aws-sdk';
import 'source-map-support/register';
import { EnvVar, EnvVarKey } from './base';
import type { EnvVar, EnvVarKey } from './base';

class EnvVarProd implements EnvVar {
ssm = new AWS.SSM();
cache = new Map<EnvVarKey, Promise<string>>();
ssm = new AWS.SSM();
cache = new Map<EnvVarKey, Promise<string>>();

private async fetch(key: EnvVarKey): Promise<string> {
const res = await this.ssm.getParameter({
Name: `/ap2020bot/${process.env.STAGE}/${key}`,
WithDecryption: true,
}).promise();
const value = res.Parameter.Value;
return value;
}
private async fetch(key: EnvVarKey): Promise<string> {
const res = await this.ssm.getParameter({
/* eslint-disable @typescript-eslint/naming-convention */
Name: `/ap2020bot/${process.env.STAGE}/${key}`,
WithDecryption: true,
/* eslint-enable @typescript-eslint/naming-convention */
}).promise();
const value = res.Parameter.Value;
return value;
}

async get(key: EnvVarKey): Promise<string> {
if (this.cache.has(key)) {
return this.cache.get(key);
}
const promise: Promise<string> = this.fetch(key);
this.cache.set(key, promise);
return promise;
async get(key: EnvVarKey): Promise<string> {
if (this.cache.has(key)) {
return this.cache.get(key);
}
const promise: Promise<string> = this.fetch(key);
this.cache.set(key, promise);
return promise;
}
}

export const envvarProd = new EnvVarProd();
22 changes: 11 additions & 11 deletions aws/src/lib/envvar/test.ts
@@ -1,19 +1,19 @@
import { EnvVar, EnvVarKey } from './base';
import type { EnvVar, EnvVarKey } from './base';

class EnvVarTest implements EnvVar {
envvars = new Map<EnvVarKey, string>();
envvars = new Map<EnvVarKey, string>();

init() {
this.envvars.clear();
}
init() {
this.envvars.clear();
}

set(key: EnvVarKey, value: string) {
this.envvars.set(key, value);
}
set(key: EnvVarKey, value: string) {
this.envvars.set(key, value);
}

get(key: EnvVarKey) {
return Promise.resolve(this.envvars.get(key));
}
get(key: EnvVarKey) {
return Promise.resolve(this.envvars.get(key));
}
}

export const envvarTest = new EnvVarTest();
2 changes: 1 addition & 1 deletion aws/src/lib/slack/client.ts
Expand Up @@ -2,5 +2,5 @@ import { WebClient } from '@slack/web-api';
import { envvar } from '../envvar';

export const slack = {
bot: (async () => (new WebClient(await envvar.get('slack/token/bot'))))(),
bot: (async () => (new WebClient(await envvar.get('slack/token/bot'))))(),
};
22 changes: 11 additions & 11 deletions aws/src/lib/slack/events/index.ts
@@ -1,14 +1,14 @@
import { envvar } from "@/lib/envvar"
import { SNSHandler } from "aws-lambda"
import { SlackEvent, SlackSNSMessage } from "./types"
import { envvar } from '@/lib/envvar';
import type { SNSHandler } from 'aws-lambda';
import type { SlackEvent, SlackSNSMessage } from './types';

export const getSlackEventTopicARN = async () => {
return `arn:aws:sns:us-east-1:${await envvar.get('aws/account-id')}:ap2020bot-${process.env.STAGE}-slack-events`
}
export const getSlackEventTopicARN = async (): Promise<string> =>
`arn:aws:sns:us-east-1:${await envvar.get('aws/account-id')}:ap2020bot-${process.env.STAGE}-slack-events`;

export const createHandler = <Event extends SlackEvent>(callback: ((message: SlackSNSMessage<Event>) => Promise<void>)): SNSHandler => {
return async (snsEvent) => {
const message = JSON.parse(snsEvent.Records[0].Sns.Message);
export const createHandler = <Event extends SlackEvent>(
callback: ((message: SlackSNSMessage<Event>) => Promise<void>),
): SNSHandler =>
async (snsEvent) => {
const message = JSON.parse(snsEvent.Records[0].Sns.Message) as SlackSNSMessage<Event>;
await callback(message);
}
}
};

0 comments on commit e87c19d

Please sign in to comment.