Selaa lähdekoodia

Initial commit

Add files from pridepack.
master
TheoryOfNekomata 2 vuotta sitten
commit
2291af4b00
17 muutettua tiedostoa jossa 3707 lisäystä ja 0 poistoa
  1. +9
    -0
      .eslintrc
  2. +108
    -0
      .gitignore
  3. +7
    -0
      LICENSE
  4. +5
    -0
      __mocks__/@theoryofnekomata/oblique-strategies-core.ts
  5. +88
    -0
      package.json
  6. +3
    -0
      pridepack.json
  7. +16
    -0
      src/index.ts
  8. +10
    -0
      src/modules/card/controller.ts
  9. +2
    -0
      src/modules/card/index.ts
  10. +9
    -0
      src/modules/card/service.ts
  11. +12
    -0
      src/routes.ts
  12. +9
    -0
      src/server.ts
  13. +53
    -0
      test/index.test.ts
  14. +21
    -0
      tsconfig.eslint.json
  15. +21
    -0
      tsconfig.json
  16. +11
    -0
      web-api.config.ts
  17. +3323
    -0
      yarn.lock

+ 9
- 0
.eslintrc Näytä tiedosto

@@ -0,0 +1,9 @@
{
"root": true,
"extends": [
"lxsmnsyc/typescript"
],
"parserOptions": {
"project": "./tsconfig.eslint.json"
}
}

+ 108
- 0
.gitignore Näytä tiedosto

@@ -0,0 +1,108 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.production
.env.development

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

.npmrc
.idea/

+ 7
- 0
LICENSE Näytä tiedosto

@@ -0,0 +1,7 @@
MIT License Copyright (c) 2022 TheoryOfNekomata <allan.crisostomo@outlook.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 5
- 0
__mocks__/@theoryofnekomata/oblique-strategies-core.ts Näytä tiedosto

@@ -0,0 +1,5 @@
import { vi } from 'vitest';

export const generate = vi.fn(() => 'foo\n-italic');

export const getDefaultCards = vi.fn(() => ['foo\n-italic']);

+ 88
- 0
package.json Näytä tiedosto

@@ -0,0 +1,88 @@
{
"name": "@theoryofnekomata/oblique-strategies-web-api",
"version": "0.0.0",
"types": "dist/types/index.d.ts",
"main": "dist/cjs/production/index.js",
"module": "dist/esm/production/index.js",
"exports": {
".": {
"development": {
"require": "./dist/cjs/development/index.js",
"import": "./dist/esm/development/index.js"
},
"require": "./dist/cjs/production/index.js",
"import": "./dist/esm/production/index.js",
"types": "./dist/types/index.d.ts"
},
"./dev": {
"production": {
"require": "./dist/cjs/production/index.js",
"import": "./dist/esm/production/index.js"
},
"require": "./dist/cjs/development/index.js",
"import": "./dist/esm/development/index.js",
"types": "./dist/types/index.d.ts"
},
"./esm": {
"development": "./dist/esm/development/index.js",
"production": "./dist/esm/production/index.js",
"default": "./dist/esm/production/index.js",
"types": "./dist/types/index.d.ts"
},
"./cjs": {
"development": "./dist/cjs/development/index.js",
"production": "./dist/cjs/production/index.js",
"default": "./dist/cjs/production/index.js",
"types": "./dist/types/index.d.ts"
}
},
"files": [
"dist",
"src"
],
"engines": {
"node": ">=10"
},
"license": "MIT",
"keywords": [
"pridepack"
],
"devDependencies": {
"@types/node": "^17.0.25",
"c8": "^7.11.2",
"eslint": "^8.13.0",
"eslint-config-lxsmnsyc": "^0.4.0",
"pridepack": "1.1.1",
"tslib": "^2.4.0",
"typescript": "^4.6.3",
"vitest": "^0.9.4"
},
"dependencies": {
"fastify": "^3.27.0"
},
"scripts": {
"prepublishOnly": "pridepack clean && pridepack build",
"build": "pridepack build",
"type-check": "pridepack check",
"lint": "pridepack lint",
"clean": "pridepack clean",
"watch": "pridepack watch",
"start": "pridepack start",
"dev": "pridepack dev",
"test": "vitest"
},
"private": false,
"description": "Web API for Oblique Strategies",
"repository": {
"url": "https://code.modal.sh/modal-soft/oblique-strategies-web-api",
"type": "git"
},
"homepage": "https://code.modal.sh/modal-soft/oblique-strategies-web-api",
"bugs": {
"url": "https://code.modal.sh/modal-soft/oblique-strategies-web-api/issues"
},
"author": "TheoryOfNekomata <allan.crisostomo@outlook.com>",
"publishConfig": {
"access": "public"
}
}

+ 3
- 0
pridepack.json Näytä tiedosto

@@ -0,0 +1,3 @@
{
"target": "es2017"
}

+ 16
- 0
src/index.ts Näytä tiedosto

@@ -0,0 +1,16 @@
import { createServer } from 'src/server';
import { addRoutes } from 'src/routes';
import * as config from 'web-api.config';

const main = (serverConfig: typeof config) => {
const server = createServer();
addRoutes(server);
server.listen(serverConfig.meta.port, serverConfig.meta.address, (err) => {
if (err) {
server.log.error(err.message);
process.exit(1);
}
});
};

main(config);

+ 10
- 0
src/modules/card/controller.ts Näytä tiedosto

@@ -0,0 +1,10 @@
import { RouteHandlerMethod } from 'fastify';
import { CardServiceImpl } from './service';

export class CardControllerImpl {
readonly generate: RouteHandlerMethod = async (request, reply) => {
const query = request.query as Record<string, unknown>;
const cardSources = query.cards as string[] | undefined;
reply.send(CardServiceImpl.generate(cardSources));
};
}

+ 2
- 0
src/modules/card/index.ts Näytä tiedosto

@@ -0,0 +1,2 @@
export * from './controller';
export * from './service';

+ 9
- 0
src/modules/card/service.ts Näytä tiedosto

@@ -0,0 +1,9 @@
import { generate as coreGenerate } from '@theoryofnekomata/oblique-strategies-core';

export interface CardService {}

export class CardServiceImpl implements CardService {
static generate(cards?: string[]) {
return coreGenerate(cards);
}
}

+ 12
- 0
src/routes.ts Näytä tiedosto

@@ -0,0 +1,12 @@
import { FastifyInstance } from 'fastify';
import { CardControllerImpl } from './modules/card';

export const addRoutes = (server: FastifyInstance) => {
const cardController = new CardControllerImpl();

server.route({
method: 'POST',
url: '/',
handler: cardController.generate,
});
};

+ 9
- 0
src/server.ts Näytä tiedosto

@@ -0,0 +1,9 @@
import fastify from 'fastify';

export type CreateServerArgs = {
env?: string
}

export const createServer = (args = { env: process.env.NODE_ENV } as CreateServerArgs) => fastify({
logger: args.env !== 'test',
});

+ 53
- 0
test/index.test.ts Näytä tiedosto

@@ -0,0 +1,53 @@
import {
describe,
it,
expect,
beforeAll,
vi,
} from 'vitest';
import { FastifyInstance } from 'fastify';
import { generate } from '@theoryofnekomata/oblique-strategies-core';
import { createServer } from '../src/server';
import { addRoutes } from '../src/routes';

vi.mock('@theoryofnekomata/oblique-strategies-core');

describe('Example', () => {
let server: FastifyInstance;

beforeAll(() => {
server = createServer();
addRoutes(server);
});

beforeAll(() => {
vi.mocked(generate).mockReturnValue('card text');
});

it('draws a card from default set', async () => {
const response = await server
.inject()
.post('/')
.headers({
Accept: 'text/plain',
});
expect(response.statusCode).toBe(200);
expect(generate).toBeCalled();
expect(response.body).toBe('card text');
});

it('draws a card from custom sets', async () => {
const response = await server
.inject()
.post('/')
.query({
cards: ['default', 'text:hello|world'],
})
.headers({
Accept: 'text/plain',
});
expect(response.statusCode).toBe(200);
expect(generate).toBeCalledWith(['default', 'text:hello|world']);
expect(response.body).toBe('card text');
});
});

+ 21
- 0
tsconfig.eslint.json Näytä tiedosto

@@ -0,0 +1,21 @@
{
"exclude": ["node_modules"],
"include": ["src", "types", "test"],
"compilerOptions": {
"module": "ESNext",
"lib": ["DOM", "ESNext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"baseUrl": ".",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"jsx": "react",
"esModuleInterop": true,
"target": "ES2017"
}
}

+ 21
- 0
tsconfig.json Näytä tiedosto

@@ -0,0 +1,21 @@
{
"exclude": ["node_modules"],
"include": ["src", "types"],
"compilerOptions": {
"module": "ESNext",
"lib": ["ESNext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"baseUrl": ".",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"jsx": "react",
"esModuleInterop": true,
"target": "ES2017"
}
}

+ 11
- 0
web-api.config.ts Näytä tiedosto

@@ -0,0 +1,11 @@
export type Meta = {
port: number | string;
env: string;
address: string;
}

export namespace meta {
export const port: (number | string) = process.env.PORT ?? 8080;
export const env = process.env.NODE_ENV ?? 'development';
export const address = process.env.ADDRESS ?? '0.0.0.0';
}

+ 3323
- 0
yarn.lock
File diff suppressed because it is too large
Näytä tiedosto


Ladataan…
Peruuta
Tallenna