-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from avra-m3/haste-1.0.0
Haste 1.0.0
- Loading branch information
Showing
43 changed files
with
5,101 additions
and
853 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { requires } from '../../../src'; | ||
import * as jwt from 'jsonwebtoken'; | ||
import { z } from 'zod'; | ||
import { HasteRequestHandler } from 'express-haste'; | ||
|
||
export const requireAuth = requires() | ||
.auth('jwt', { | ||
type: 'apiKey', | ||
scheme: 'Bearer', | ||
}) | ||
.response('401', z.object({ message: z.literal('Unauthorized') })); | ||
|
||
const splitBearer = z.tuple([z.literal('Bearer'), z.string()]); | ||
const tokenHeaderSchema = z | ||
.string() | ||
.transform((value) => splitBearer.parse(value.split(' ', 1))[1]); | ||
/** | ||
* This is NOT intended to be a reference for implementing secure jwt validation. | ||
* This example is vastly oversimplified and inherently insecure, | ||
* for details on a proper jwt implementation see https://www.npmjs.com/package/jsonwebtoken | ||
*/ | ||
export const authValidator: HasteRequestHandler<typeof requireAuth> = (req, res, next) => { | ||
try { | ||
const rawToken = req.headers['authorization']; | ||
const probablyToken = tokenHeaderSchema.parse(rawToken); | ||
// IMPORTANT: this is being verified with a symmetric key, do not use in a real application | ||
const token = jwt.verify(probablyToken, 'totally very secret'); | ||
if (token) { | ||
req.app.set('user', token) | ||
next(); | ||
} | ||
} catch (e) { | ||
res.status(401).json({ | ||
message: 'Unauthorized', | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as H from '../../../src'; | ||
import 'express-haste'; | ||
|
||
jest.mock('express-haste'); | ||
|
||
|
||
describe('customErrorHandlingAndAuth', () => { | ||
it('should pass', () => { | ||
/** End-to-End Tests coming soon TM **/ | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import express, { json, Router } from 'express'; | ||
import { HasteRequestHandler } from 'express-haste'; | ||
import cookieParser from 'cookie-parser'; | ||
import { HasteCustomErrorHandler, requires } from '../../../src'; | ||
import { z } from 'zod'; | ||
import { authValidator, requireAuth } from './auth'; | ||
|
||
const app: express.Express = express(); | ||
|
||
app.use(json()); | ||
app.use(cookieParser()); | ||
/** | ||
* In this example we have 2 routes | ||
* /public -> returns {message: "hello world"} | ||
* /user -> accepts a jwt and returns the user object | ||
* /docs -> view the documentation (no authentication) | ||
*/ | ||
|
||
const docRouter = Router(); | ||
app.use('/docs', docRouter); | ||
|
||
const customErrorFunction: HasteCustomErrorHandler = (e, res) => | ||
res.send({ | ||
message: e.issues.map((i) => i.message).join(' and '), | ||
}); | ||
|
||
const r = () => requires({ errorHandler: customErrorFunction }); | ||
|
||
// Get one pet is exempt from needing a header for demo reasons. | ||
app.get('/public', r().response('200', z.object({ message: z.string() })), (_req, res) => | ||
res.json({ | ||
message: 'hello world', | ||
}) | ||
); | ||
|
||
// Require an authorization header for requests not public | ||
app.use(requireAuth, authValidator); | ||
|
||
app.get('/user', r().response('200', z.object({}).passthrough().describe('Anything in the JWT'))); | ||
app.post('/user'); | ||
|
||
const updateRequirements = requires({ errorHandler: customErrorFunction }) | ||
.body(z.object({ email: z.string() }).describe('Update the email of this user')) | ||
.response('202', z.object({ message: z.literal('Accepted') })); | ||
const updateUser: HasteRequestHandler<typeof updateRequirements> = (_req, res) => res.status(202); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
coveragePathIgnorePatterns: ['/node_modules/', '/dist/'], | ||
modulePathIgnorePatterns: ['/node_modules/', '/dist/'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "simple-haste-example", | ||
"version": "1.0.0", | ||
"main": "index.ts", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "ts-node ./serve.ts", | ||
"test:ci": "jest" | ||
}, | ||
"dependencies": { | ||
"@types/jsonwebtoken": "^9.0.5", | ||
"body-parser": "^1.20.2", | ||
"cookie-parser": "^1.4.6", | ||
"express": "^4.18.2", | ||
"express-haste": "1.0.0-canary.0", | ||
"jsonwebtoken": "^9.0.2", | ||
"zod": "^3.22.4" | ||
}, | ||
"devDependencies": { | ||
"jest": "^29.7.0", | ||
"ts-node": "^10.9.2" | ||
}, | ||
"overrides": { | ||
"@hookform/resolvers": "^3.3.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export const getRedocHtml = ({ apiPath }: { apiPath: string }) => | ||
` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Example Pet App docs</title> | ||
<!-- needed for adaptive design --> | ||
<meta charset="utf-8"/> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet"> | ||
<!-- | ||
Redoc doesn't change outer page styles | ||
--> | ||
<style> | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<redoc spec-url='{{apiPath}}'></redoc> | ||
<script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script> | ||
</body> | ||
</html> | ||
` | ||
.replace('{{apiPath}}', apiPath) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import app from "./index"; | ||
|
||
app.listen(3000, () => { | ||
console.log('Check out http://localhost:3000/docs') | ||
}) |
Oops, something went wrong.