Skip to content

Commit

Permalink
refactor(app-express): bump deps. Full test harness. organize main
Browse files Browse the repository at this point in the history
  • Loading branch information
TillaTheHun0 committed Dec 5, 2023
1 parent 0401495 commit f93e778
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 93 deletions.
2 changes: 1 addition & 1 deletion packages/app-express/api/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { crawler } from './crawler.ts'

const { when, compose } = R

export const mountServicesWith = (services: HyperServices) => (app: Server) => {
export const withCoreServicesRoutes = (services: HyperServices) => (app: Server) => {
const hasService = (service: keyof HyperServices) => !!services[service]

return compose(
Expand Down
86 changes: 43 additions & 43 deletions packages/app-express/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/app-express/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export {
readableStreamFromIterable,
readableStreamFromReader,
} from 'https://deno.land/[email protected]/streams/mod.ts'
export { contentType as getMimeType } from 'https://deno.land/std@0.207.0/media_types/mod.ts'
export { contentType as getMimeType } from 'https://deno.land/std@0.208.0/media_types/mod.ts'

export { isHyperErr } from 'https://raw.githubusercontent.com/hyper63/hyper/hyper-utils%40v0.1.2/packages/utils/mod.js'
2 changes: 1 addition & 1 deletion packages/app-express/dev_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export {
assert,
assertEquals,
assertObjectMatch,
} from 'https://deno.land/std@0.207.0/assert/mod.ts'
} from 'https://deno.land/std@0.208.0/assert/mod.ts'
67 changes: 59 additions & 8 deletions packages/app-express/harness.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,66 @@
// Load .env
import 'https://deno.land/std@0.207.0/dotenv/load.ts'
import 'https://deno.land/std@0.208.0/dotenv/load.ts'

import { default as core } from 'https://raw.githubusercontent.com/hyper63/hyper/hyper%40v4.3.1/packages/core/mod.ts'
import namespacedS3 from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-namespaced-s3/v3.0.0/mod.js'
import { join } from 'https://deno.land/[email protected]/path/mod.ts'

import { default as core } from 'https://raw.githubusercontent.com/hyper63/hyper/hyper%40v4.3.2/packages/core/mod.ts'

import { default as sqlite } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-sqlite/v2.0.12/mod.js'
import { default as fs } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-fs/v3.0.2/mod.js'
import { default as minisearch } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-minisearch/v2.1.4/mod.js'
import { default as mongodb } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-mongodb/v3.3.0/mod.ts'
import { default as queue } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-queue/v0.3.1/mod.js'

import app from './mod.ts'

const hyperConfig = {
app,
adapters: [{ port: 'storage' as const, plugins: [namespacedS3('express-harness')] }],
middleware: [],
async function mkdir(DIR: string) {
try {
return await Deno.mkdir(DIR, { recursive: true })
} catch (err) {
if (err instanceof Deno.errors.AlreadyExists) {
// already exists so return
return true
} else {
// unexpected error, maybe permissions, pass it along
throw err
}
}
}

async function main() {
const DIR = join('.', '__hyper__')

await mkdir(DIR)

const hyperConfig = {
app,
adapters: [
{
port: 'data' as const,
plugins: [mongodb({ dir: DIR, dirVersion: '7.0.4' })],
},
{
port: 'cache' as const,
plugins: [sqlite({ dir: DIR })],
},
{
port: 'storage' as const,
plugins: [fs({ dir: DIR })],
},
{
port: 'search' as const,
plugins: [minisearch({ dir: DIR })],
},
{
port: 'queue' as const,
// @ts-ignore name is actually optional, so this error can be ignored. TODO: update the types
plugins: [queue({ dir: DIR })],
},
],
middleware: [],
}

return core(hyperConfig)
}

core(hyperConfig)
main()
88 changes: 49 additions & 39 deletions packages/app-express/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { mountServicesWith } from './api/mod.ts'
import { cors, express, helmet } from './deps.ts'
import { withCoreServicesRoutes } from './api/mod.ts'
import { cors, express, helmet, R } from './deps.ts'
import type { ErrorRouteHandler, HyperServices, Server } from './types.ts'

const { pipe } = R

// All of these args need to be specified, or it won't be invoked on error
const errorHandler: ErrorRouteHandler = (err, _req, res, _next) => {
if (err) {
Expand All @@ -17,43 +19,51 @@ const errorHandler: ErrorRouteHandler = (err, _req, res, _next) => {
}

export function main(services: HyperServices): Server {
const mountServices = mountServicesWith(services)

let app = express()

app.use(helmet())
app.use(cors({ credentials: true }))

if (services.middleware?.length) {
app = services.middleware
.reverse()
.reduce(
(app, middleware) => middleware(app, services),
app,
)
}
return pipe(
/**
* Apply base layer middleware
*/
(app) => app.use(helmet()),
(app) => app.use(cors({ credentials: true })),
/**
* Apply middleware to the app
*/
(app) => {
if (!services.middleware?.length) return app

app = mountServices(app)
return services.middleware
.reverse()
.reduce(
(app, middleware) => middleware(app, services),
app,
)
},
/**
* Apply hyper core services routes
*/
withCoreServicesRoutes(services),
/**
* Apply root route
*/
(app) => {
app.get('/', (_req, res) => {
res.json({
name: 'hyper',
version: '1.0-beta',
services: Object.keys(services)
.filter((k) => k !== 'events')
.filter((k) => k !== 'middleware')
.filter((k) => k !== 'hooks')
.filter((k) => services[k as keyof HyperServices] !== null),
})
})

app.get('/', (_req, res) => {
res.json({
name: 'hyper',
version: '1.0-beta',
services: Object.keys(services)
.filter((k) => k !== 'events')
.filter((k) => k !== 'middleware')
.filter((k) => k !== 'hooks')
.filter((k) => services[k as keyof HyperServices] !== null),
})
})

// Error Handler
app.use(errorHandler)

// Handle fall through case, if not handled above then return 404
app.use((_req, res) => {
res.status(404).json({ ok: false, msg: 'not found!' })
})

return app
return app
},
(app) => app.use(errorHandler),
(app) =>
app.use((_req, res) => {
res.status(404).json({ ok: false, msg: 'not found!' })
}),
)(express())
}

0 comments on commit f93e778

Please sign in to comment.