mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-22 12:41:21 +00:00
fix types and build
This commit is contained in:
parent
b920c1f7ad
commit
6c1a1277ff
10 changed files with 783 additions and 76 deletions
18
package.json
18
package.json
|
@ -60,6 +60,8 @@
|
||||||
"ts-auto-mock": "^3.6.2",
|
"ts-auto-mock": "^3.6.2",
|
||||||
"ts-jest": "^28.0.3",
|
"ts-jest": "^28.0.3",
|
||||||
"ts-node": "^9.1.1",
|
"ts-node": "^9.1.1",
|
||||||
|
"tsconfig-paths": "^4.0.0",
|
||||||
|
"tscpaths": "^0.0.9",
|
||||||
"typescript": "^4.6.4"
|
"typescript": "^4.6.4"
|
||||||
},
|
},
|
||||||
"bundledDependencies": [],
|
"bundledDependencies": [],
|
||||||
|
@ -72,12 +74,11 @@
|
||||||
"static"
|
"static"
|
||||||
],
|
],
|
||||||
"nodemonConfig": {
|
"nodemonConfig": {
|
||||||
"ignore":
|
"ignore": [
|
||||||
[
|
"test/**/*.test.ts",
|
||||||
"test/**/*.test.ts",
|
".git",
|
||||||
".git",
|
"node_modules"
|
||||||
"node_modules"
|
],
|
||||||
],
|
|
||||||
"watch": [
|
"watch": [
|
||||||
"src"
|
"src"
|
||||||
],
|
],
|
||||||
|
@ -89,9 +90,10 @@
|
||||||
"remove:files": "rimraf dist",
|
"remove:files": "rimraf dist",
|
||||||
"test:unit": "jest --config config/jest.config.js",
|
"test:unit": "jest --config config/jest.config.js",
|
||||||
"build:nostatic": "yarn remove:files && tsc --project ./",
|
"build:nostatic": "yarn remove:files && tsc --project ./",
|
||||||
"build": "yarn remove:files && yarn copy:files && tsc --project ./",
|
"build:typescript": "tsc --project tsconfig.json && tscpaths -p tsconfig.json -s ./src -o ./dist",
|
||||||
|
"build": "yarn remove:files && yarn copy:files && yarn build:typescript",
|
||||||
|
"start": "node -r tsconfig-paths/register -r ts-node ./dist/src/server.js",
|
||||||
"dev": "nodemon",
|
"dev": "nodemon",
|
||||||
"start": "node dist/src/server.js",
|
|
||||||
"lint": "eslint src --fix",
|
"lint": "eslint src --fix",
|
||||||
"types:check": "tsc --noEmit --pretty",
|
"types:check": "tsc --noEmit --pretty",
|
||||||
"pretty": "prettier --write src"
|
"pretty": "prettier --write src"
|
||||||
|
|
|
@ -2,10 +2,10 @@ import { Request, Response } from 'express'
|
||||||
import * as winston from 'winston'
|
import * as winston from 'winston'
|
||||||
import Busboy from 'busboy'
|
import Busboy from 'busboy'
|
||||||
import type { Config } from 'src/types/config'
|
import type { Config } from 'src/types/config'
|
||||||
import type { Store } from 'src/types/callback'
|
|
||||||
import type { Document } from 'src/types/document'
|
import type { Document } from 'src/types/document'
|
||||||
import constants from 'src/constants'
|
import constants from 'src/constants'
|
||||||
import KeyGenerator from 'src/lib/key-generators'
|
import KeyGenerator from 'src/lib/key-generators'
|
||||||
|
import { Store } from '../document-stores'
|
||||||
|
|
||||||
class DocumentHandler {
|
class DocumentHandler {
|
||||||
keyLength: number
|
keyLength: number
|
||||||
|
|
|
@ -1,51 +1,12 @@
|
||||||
import type {
|
import type { Config } from 'src/types/config'
|
||||||
AmazonStoreConfig,
|
import { Store } from '.'
|
||||||
BaseStoreConfig,
|
|
||||||
Config,
|
|
||||||
GoogleStoreConfig,
|
|
||||||
MemcachedStoreConfig,
|
|
||||||
MongoStoreConfig,
|
|
||||||
PostgresStoreConfig,
|
|
||||||
RedisStoreConfig,
|
|
||||||
RethinkDbStoreConfig
|
|
||||||
} from 'src/types/config'
|
|
||||||
import type { Store } from 'src/types/callback'
|
|
||||||
import { StoreNames } from 'src/types/store-names'
|
|
||||||
|
|
||||||
const build = async (config: Config): Promise<Store> => {
|
const build = async (config: Config): Promise<Store> => {
|
||||||
const DocumentStore = (await import(`../document-stores/${config.storeName}`))
|
const DocumentStore = (
|
||||||
.default
|
await import(`../document-stores/${config.storage.type}`)
|
||||||
|
).default
|
||||||
|
|
||||||
let store: BaseStoreConfig
|
return new DocumentStore(config.storage)
|
||||||
|
|
||||||
switch (config.storeName) {
|
|
||||||
case StoreNames.amazons3:
|
|
||||||
store = config.storage as AmazonStoreConfig
|
|
||||||
break
|
|
||||||
case StoreNames.googledatastore:
|
|
||||||
store = config.storage as GoogleStoreConfig
|
|
||||||
break
|
|
||||||
case StoreNames.memcached:
|
|
||||||
store = config.storage as MemcachedStoreConfig
|
|
||||||
break
|
|
||||||
case StoreNames.mongo:
|
|
||||||
store = config.storage as MongoStoreConfig
|
|
||||||
break
|
|
||||||
case StoreNames.postgres:
|
|
||||||
store = config.storage as PostgresStoreConfig
|
|
||||||
break
|
|
||||||
case StoreNames.redis:
|
|
||||||
store = config.storage as RedisStoreConfig
|
|
||||||
break
|
|
||||||
case StoreNames.rethinkdb:
|
|
||||||
store = config.storage as RethinkDbStoreConfig
|
|
||||||
break
|
|
||||||
case StoreNames.file:
|
|
||||||
default:
|
|
||||||
store = config.storage as BaseStoreConfig
|
|
||||||
break
|
|
||||||
}
|
|
||||||
return new DocumentStore(store)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default build
|
export default build
|
||||||
|
|
|
@ -17,8 +17,8 @@ const getConfig = (): Config => {
|
||||||
config.storage = {}
|
config.storage = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config.storeName) {
|
if (!config.storage.type) {
|
||||||
config.storeName = 'file'
|
config.storage.type = 'file'
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
|
@ -4,4 +4,4 @@ export const getStaticDirectory = (baseDirectory: string) =>
|
||||||
path.join(baseDirectory, '..', 'static')
|
path.join(baseDirectory, '..', 'static')
|
||||||
|
|
||||||
export const getStaticItemDirectory = (baseDirectory: string, item: string) =>
|
export const getStaticItemDirectory = (baseDirectory: string, item: string) =>
|
||||||
path.join(getStaticDirectory(baseDirectory), item)
|
path.join(baseDirectory, '..', 'static', item)
|
||||||
|
|
|
@ -12,36 +12,40 @@ export interface Config {
|
||||||
logging: Logging[]
|
logging: Logging[]
|
||||||
keyGenerator: KeyGeneratorConfig
|
keyGenerator: KeyGeneratorConfig
|
||||||
rateLimits: RateLimits
|
rateLimits: RateLimits
|
||||||
storage: unknown
|
storage: StoreConfig
|
||||||
documents: Record<string, string>
|
documents: Record<string, string>
|
||||||
storeName: StoreNames
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export type BaseStoreConfig = {
|
export type BaseStoreConfig = {
|
||||||
type: string
|
type: StoreNames
|
||||||
expire?: number
|
expire?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MongoStoreConfig extends BaseStoreConfig {
|
export interface MongoStoreConfig extends BaseStoreConfig {
|
||||||
connectionUrl: string
|
connectionUrl: string
|
||||||
|
type: StoreNames.mongo
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MemcachedStoreConfig extends BaseStoreConfig {
|
export interface MemcachedStoreConfig extends BaseStoreConfig {
|
||||||
host: string
|
host: string
|
||||||
port: number
|
port: number
|
||||||
|
type: StoreNames.memcached
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface FileStoreConfig extends BaseStoreConfig {
|
export interface FileStoreConfig extends BaseStoreConfig {
|
||||||
path: string
|
path: string
|
||||||
|
type: StoreNames.file
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AmazonStoreConfig extends BaseStoreConfig {
|
export interface AmazonStoreConfig extends BaseStoreConfig {
|
||||||
bucket: string
|
bucket: string
|
||||||
region: string
|
region: string
|
||||||
|
type: StoreNames.amazons3
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PostgresStoreConfig extends BaseStoreConfig {
|
export interface PostgresStoreConfig extends BaseStoreConfig {
|
||||||
connectionUrl: string
|
connectionUrl: string
|
||||||
|
type: StoreNames.postgres
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RethinkDbStoreConfig extends BaseStoreConfig {
|
export interface RethinkDbStoreConfig extends BaseStoreConfig {
|
||||||
|
@ -50,6 +54,7 @@ export interface RethinkDbStoreConfig extends BaseStoreConfig {
|
||||||
db: string
|
db: string
|
||||||
user: string
|
user: string
|
||||||
password: string
|
password: string
|
||||||
|
type: StoreNames.rethinkdb
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RedisStoreConfig extends BaseStoreConfig {
|
export interface RedisStoreConfig extends BaseStoreConfig {
|
||||||
|
@ -60,9 +65,22 @@ export interface RedisStoreConfig extends BaseStoreConfig {
|
||||||
password?: string
|
password?: string
|
||||||
host?: string
|
host?: string
|
||||||
port?: string
|
port?: string
|
||||||
|
type: StoreNames.redis
|
||||||
}
|
}
|
||||||
|
|
||||||
export type GoogleStoreConfig = BaseStoreConfig
|
export interface GoogleStoreConfig extends BaseStoreConfig {
|
||||||
|
type: StoreNames.googledatastore
|
||||||
|
}
|
||||||
|
|
||||||
|
export type StoreConfig =
|
||||||
|
| MongoStoreConfig
|
||||||
|
| MemcachedStoreConfig
|
||||||
|
| FileStoreConfig
|
||||||
|
| AmazonStoreConfig
|
||||||
|
| PostgresStoreConfig
|
||||||
|
| RethinkDbStoreConfig
|
||||||
|
| RedisStoreConfig
|
||||||
|
| GoogleStoreConfig
|
||||||
|
|
||||||
export interface KeyGeneratorConfig {
|
export interface KeyGeneratorConfig {
|
||||||
type: string
|
type: string
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
import { Store } from 'src/lib/document-stores'
|
||||||
import KeyGenerator from 'src/lib/key-generators'
|
import KeyGenerator from 'src/lib/key-generators'
|
||||||
import type { Config } from './config'
|
import type { Config } from './config'
|
||||||
import type { Store } from './callback'
|
|
||||||
|
|
||||||
export type Document = {
|
export type Document = {
|
||||||
store: Store
|
store: Store
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import RedisDocumentStore from 'src/lib/document-stores/redis'
|
import RedisDocumentStore from 'src/lib/document-stores/redis'
|
||||||
|
import { StoreNames } from 'src/types/store-names'
|
||||||
|
|
||||||
describe('Redis document store', () => {
|
describe('Redis document store', () => {
|
||||||
let store: RedisDocumentStore
|
let store: RedisDocumentStore
|
||||||
|
@ -13,7 +14,7 @@ describe('Redis document store', () => {
|
||||||
it('should be able to set a key and have an expiration set', async () => {
|
it('should be able to set a key and have an expiration set', async () => {
|
||||||
store = new RedisDocumentStore({
|
store = new RedisDocumentStore({
|
||||||
expire: 10,
|
expire: 10,
|
||||||
type: 'redis',
|
type: StoreNames.redis
|
||||||
})
|
})
|
||||||
return store.set('hello1', 'world', async () => {
|
return store.set('hello1', 'world', async () => {
|
||||||
const res = await store.client?.ttl('hello1')
|
const res = await store.client?.ttl('hello1')
|
||||||
|
@ -24,7 +25,7 @@ describe('Redis document store', () => {
|
||||||
it('should not set an expiration when told not to', async () => {
|
it('should not set an expiration when told not to', async () => {
|
||||||
store = new RedisDocumentStore({
|
store = new RedisDocumentStore({
|
||||||
expire: 10,
|
expire: 10,
|
||||||
type: 'redis',
|
type: StoreNames.redis
|
||||||
})
|
})
|
||||||
|
|
||||||
store.set(
|
store.set(
|
||||||
|
@ -34,13 +35,13 @@ describe('Redis document store', () => {
|
||||||
const res = await store.client?.ttl('hello2')
|
const res = await store.client?.ttl('hello2')
|
||||||
expect(res).toEqual(-1)
|
expect(res).toEqual(-1)
|
||||||
},
|
},
|
||||||
true,
|
true
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not set an expiration when expiration is off', async () => {
|
it('should not set an expiration when expiration is off', async () => {
|
||||||
store = new RedisDocumentStore({
|
store = new RedisDocumentStore({
|
||||||
type: 'redis',
|
type: StoreNames.redis
|
||||||
})
|
})
|
||||||
|
|
||||||
store.set('hello3', 'world', async () => {
|
store.set('hello3', 'world', async () => {
|
||||||
|
|
|
@ -1,8 +1,4 @@
|
||||||
{
|
{
|
||||||
"ts-node": {
|
|
||||||
"files": true
|
|
||||||
},
|
|
||||||
"files": ["src/global.d.ts"],
|
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"composite": false,
|
"composite": false,
|
||||||
|
@ -22,14 +18,14 @@
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"typeRoots": ["node_modules/@types", "src/global.d.ts"],
|
"typeRoots": ["node_modules/@types", "src/global.d.ts"],
|
||||||
"target": "es6",
|
"target": "es2021",
|
||||||
"noEmit": false,
|
"noEmit": false,
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"rootDir": ".",
|
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"paths": {
|
"paths": {
|
||||||
|
"*": ["node_modules/*"],
|
||||||
"src/*": ["./src/*"]
|
"src/*": ["./src/*"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue