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-jest": "^28.0.3",
|
||||
"ts-node": "^9.1.1",
|
||||
"tsconfig-paths": "^4.0.0",
|
||||
"tscpaths": "^0.0.9",
|
||||
"typescript": "^4.6.4"
|
||||
},
|
||||
"bundledDependencies": [],
|
||||
|
@ -72,12 +74,11 @@
|
|||
"static"
|
||||
],
|
||||
"nodemonConfig": {
|
||||
"ignore":
|
||||
[
|
||||
"test/**/*.test.ts",
|
||||
".git",
|
||||
"node_modules"
|
||||
],
|
||||
"ignore": [
|
||||
"test/**/*.test.ts",
|
||||
".git",
|
||||
"node_modules"
|
||||
],
|
||||
"watch": [
|
||||
"src"
|
||||
],
|
||||
|
@ -89,9 +90,10 @@
|
|||
"remove:files": "rimraf dist",
|
||||
"test:unit": "jest --config config/jest.config.js",
|
||||
"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",
|
||||
"start": "node dist/src/server.js",
|
||||
"lint": "eslint src --fix",
|
||||
"types:check": "tsc --noEmit --pretty",
|
||||
"pretty": "prettier --write src"
|
||||
|
|
|
@ -2,10 +2,10 @@ import { Request, Response } from 'express'
|
|||
import * as winston from 'winston'
|
||||
import Busboy from 'busboy'
|
||||
import type { Config } from 'src/types/config'
|
||||
import type { Store } from 'src/types/callback'
|
||||
import type { Document } from 'src/types/document'
|
||||
import constants from 'src/constants'
|
||||
import KeyGenerator from 'src/lib/key-generators'
|
||||
import { Store } from '../document-stores'
|
||||
|
||||
class DocumentHandler {
|
||||
keyLength: number
|
||||
|
|
|
@ -1,51 +1,12 @@
|
|||
import type {
|
||||
AmazonStoreConfig,
|
||||
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'
|
||||
import type { Config } from 'src/types/config'
|
||||
import { Store } from '.'
|
||||
|
||||
const build = async (config: Config): Promise<Store> => {
|
||||
const DocumentStore = (await import(`../document-stores/${config.storeName}`))
|
||||
.default
|
||||
const DocumentStore = (
|
||||
await import(`../document-stores/${config.storage.type}`)
|
||||
).default
|
||||
|
||||
let store: BaseStoreConfig
|
||||
|
||||
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)
|
||||
return new DocumentStore(config.storage)
|
||||
}
|
||||
|
||||
export default build
|
||||
|
|
|
@ -17,8 +17,8 @@ const getConfig = (): Config => {
|
|||
config.storage = {}
|
||||
}
|
||||
|
||||
if (!config.storeName) {
|
||||
config.storeName = 'file'
|
||||
if (!config.storage.type) {
|
||||
config.storage.type = 'file'
|
||||
}
|
||||
|
||||
return config
|
||||
|
|
|
@ -4,4 +4,4 @@ export const getStaticDirectory = (baseDirectory: string) =>
|
|||
path.join(baseDirectory, '..', 'static')
|
||||
|
||||
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[]
|
||||
keyGenerator: KeyGeneratorConfig
|
||||
rateLimits: RateLimits
|
||||
storage: unknown
|
||||
storage: StoreConfig
|
||||
documents: Record<string, string>
|
||||
storeName: StoreNames
|
||||
}
|
||||
|
||||
export type BaseStoreConfig = {
|
||||
type: string
|
||||
type: StoreNames
|
||||
expire?: number
|
||||
}
|
||||
|
||||
export interface MongoStoreConfig extends BaseStoreConfig {
|
||||
connectionUrl: string
|
||||
type: StoreNames.mongo
|
||||
}
|
||||
|
||||
export interface MemcachedStoreConfig extends BaseStoreConfig {
|
||||
host: string
|
||||
port: number
|
||||
type: StoreNames.memcached
|
||||
}
|
||||
|
||||
export interface FileStoreConfig extends BaseStoreConfig {
|
||||
path: string
|
||||
type: StoreNames.file
|
||||
}
|
||||
|
||||
export interface AmazonStoreConfig extends BaseStoreConfig {
|
||||
bucket: string
|
||||
region: string
|
||||
type: StoreNames.amazons3
|
||||
}
|
||||
|
||||
export interface PostgresStoreConfig extends BaseStoreConfig {
|
||||
connectionUrl: string
|
||||
type: StoreNames.postgres
|
||||
}
|
||||
|
||||
export interface RethinkDbStoreConfig extends BaseStoreConfig {
|
||||
|
@ -50,6 +54,7 @@ export interface RethinkDbStoreConfig extends BaseStoreConfig {
|
|||
db: string
|
||||
user: string
|
||||
password: string
|
||||
type: StoreNames.rethinkdb
|
||||
}
|
||||
|
||||
export interface RedisStoreConfig extends BaseStoreConfig {
|
||||
|
@ -60,9 +65,22 @@ export interface RedisStoreConfig extends BaseStoreConfig {
|
|||
password?: string
|
||||
host?: 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 {
|
||||
type: string
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { Store } from 'src/lib/document-stores'
|
||||
import KeyGenerator from 'src/lib/key-generators'
|
||||
import type { Config } from './config'
|
||||
import type { Store } from './callback'
|
||||
|
||||
export type Document = {
|
||||
store: Store
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import RedisDocumentStore from 'src/lib/document-stores/redis'
|
||||
import { StoreNames } from 'src/types/store-names'
|
||||
|
||||
describe('Redis document store', () => {
|
||||
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 () => {
|
||||
store = new RedisDocumentStore({
|
||||
expire: 10,
|
||||
type: 'redis',
|
||||
type: StoreNames.redis
|
||||
})
|
||||
return store.set('hello1', 'world', async () => {
|
||||
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 () => {
|
||||
store = new RedisDocumentStore({
|
||||
expire: 10,
|
||||
type: 'redis',
|
||||
type: StoreNames.redis
|
||||
})
|
||||
|
||||
store.set(
|
||||
|
@ -34,13 +35,13 @@ describe('Redis document store', () => {
|
|||
const res = await store.client?.ttl('hello2')
|
||||
expect(res).toEqual(-1)
|
||||
},
|
||||
true,
|
||||
true
|
||||
)
|
||||
})
|
||||
|
||||
it('should not set an expiration when expiration is off', async () => {
|
||||
store = new RedisDocumentStore({
|
||||
type: 'redis',
|
||||
type: StoreNames.redis
|
||||
})
|
||||
|
||||
store.set('hello3', 'world', async () => {
|
||||
|
|
|
@ -1,8 +1,4 @@
|
|||
{
|
||||
"ts-node": {
|
||||
"files": true
|
||||
},
|
||||
"files": ["src/global.d.ts"],
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"composite": false,
|
||||
|
@ -22,14 +18,14 @@
|
|||
"skipLibCheck": true,
|
||||
"strict": true,
|
||||
"typeRoots": ["node_modules/@types", "src/global.d.ts"],
|
||||
"target": "es6",
|
||||
"target": "es2021",
|
||||
"noEmit": false,
|
||||
"module": "commonjs",
|
||||
"sourceMap": true,
|
||||
"rootDir": ".",
|
||||
"outDir": "dist",
|
||||
"baseUrl": ".",
|
||||
"paths": {
|
||||
"*": ["node_modules/*"],
|
||||
"src/*": ["./src/*"]
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Reference in a new issue