Skip to content

Commit a78fa61

Browse files
authored
extendRouter in auth0 and GH sim (#341)
* extendRouter in auth0 and GH sim * lint
1 parent c138f41 commit a78fa61

File tree

11 files changed

+148
-63
lines changed

11 files changed

+148
-63
lines changed

.changes/extend-auth0-gh-router.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@simulacrum/auth0-simulator": patch:bug
3+
"@simulacrum/github-api-simulator": patch:bug
4+
---
5+
6+
Use the `extend` options and properly pass the `extendRouter` that will allow the user to add or change route handling. Closes #317.

packages/auth0/example/index.mts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { simulation } from "../src/index.ts";
2+
3+
let app = simulation({
4+
extend: {
5+
extendRouter: (router, _simulationStore) => {
6+
router.get("/hello", (_req, res) => {
7+
res.status(200).json({ message: "Hello from Auth0 simulator!" });
8+
});
9+
},
10+
},
11+
});
12+
app.listen(undefined, () =>
13+
console.log(
14+
`auth0 simulation server started at https://localhost:4400\nusername: [email protected]\npassword: 12345\n`
15+
)
16+
);

packages/auth0/example/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/auth0/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"main": "./dist/index.cjs",
66
"bin": "bin/start.cjs",
77
"scripts": {
8-
"start": "node --import=tsx ./example/index.ts",
9-
"start:bin": "node ./bin/start.js",
8+
"start": "node --import=tsx ./example/index.mts",
9+
"start:bin": "node ./bin/start.cjs",
1010
"test": "NODE_EXTRA_CA_CERTS=\"$(mkcert -CAROOT)/rootCA.pem\" vitest run --fileParallelism=false",
1111
"test:watch": "NODE_EXTRA_CA_CERTS=\"$(mkcert -CAROOT)/rootCA.pem\" vitest watch --fileParallelism=false",
1212
"prepack": "npm run build",

packages/auth0/src/auth/jwt.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { SignOptions } from "jsonwebtoken";
2-
import { sign } from "jsonwebtoken";
2+
import * as jwt from "jsonwebtoken";
33
import { JWKS, PRIVATE_KEY } from "./constants.ts";
44

55
export const parseKey = (key: string): string => key.split("~~").join("\n");
66

7-
type SignPayload = Parameters<typeof sign>[0];
7+
type SignPayload = Parameters<typeof jwt.sign>[0];
88

99
export function createJsonWebToken<P extends SignPayload>(
1010
payload: P,
@@ -14,5 +14,5 @@ export function createJsonWebToken<P extends SignPayload>(
1414
keyid: JWKS.keys[0].kid,
1515
}
1616
): string {
17-
return sign(payload, privateKey, options);
17+
return jwt.sign(payload, privateKey, options);
1818
}

packages/auth0/src/handlers/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import express, { type Request, type Express } from "express";
1+
import express, { type Request, type Express, Router } from "express";
22
import type { ExtendedSimulationStore } from "../store/index.ts";
33
import { createCors } from "../middleware/create-cors.ts";
44
import { noCache } from "../middleware/no-cache.ts";
@@ -11,7 +11,13 @@ import { type Auth0Configuration } from "../types.ts";
1111

1212
const publicDir = path.join(import.meta.dirname, "..", "views", "public");
1313
export const extendRouter =
14-
(config: Auth0Configuration, debug = false) =>
14+
(
15+
config: Auth0Configuration,
16+
extend:
17+
| ((router: Router, simulationStore: ExtendedSimulationStore) => void)
18+
| undefined,
19+
debug = false
20+
) =>
1521
(router: Express, simulationStore: ExtendedSimulationStore) => {
1622
const serviceURL = (request: Request) =>
1723
`${request.protocol}://${request.get("Host")}/`;
@@ -27,7 +33,13 @@ export const extendRouter =
2733
.use(express.static(publicDir))
2834
.use(createSession())
2935
.use(createCors())
30-
.use(noCache())
36+
.use(noCache());
37+
38+
if (extend) {
39+
extend(router, simulationStore);
40+
}
41+
42+
router
3143
.get("/health", (_, response) => {
3244
response.send({ status: "ok" });
3345
})

packages/auth0/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ export const simulation: Auth0Simulator = (args = {}) => {
3737
? undefined
3838
: auth0InitialStoreSchema.parse(args?.initialState);
3939
return createFoundationSimulationServer({
40-
port: 4400, // default port
40+
port: config.port ?? 4400, // default port
4141
protocol: "https",
4242
extendStore: extendStore(parsedInitialState, args?.extend?.extendStore),
43-
extendRouter: extendRouter(config, args.debug),
43+
extendRouter: extendRouter(config, args.extend?.extendRouter, args.debug),
4444
})();
4545
};
4646

packages/auth0/test/auth0.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,27 @@ describe("Auth0 simulator", () => {
4040
let frontendServer: FoundationSimulatorListening<unknown>;
4141
beforeAll(async () => {
4242
frontendServer = await frontendSimulation.listen();
43-
const app = simulation();
43+
const app = simulation({
44+
extend: {
45+
extendRouter: (router) => {
46+
router.get("/hello-world", (_req, res) => {
47+
res.send("Hello World");
48+
});
49+
},
50+
},
51+
});
4452
server = await app.listen(basePort);
4553
});
4654
afterAll(async () => {
4755
await server.ensureClose();
4856
await frontendServer.ensureClose();
4957
});
5058

59+
it("allows extending the router", async () => {
60+
let res: Response = await fetch(`${auth0Url}/hello-world`);
61+
expect(res.ok).toBe(true);
62+
});
63+
5164
describe("/authorize", () => {
5265
describe("response_mode=query", () => {
5366
it("has a heartbeat", async () => {

packages/github-api/src/extend-api.ts

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,58 @@ import { stringify } from "querystring";
33
import { createHandler } from "./graphql/handler.ts";
44
import type { ExtendedSimulationStore } from "./store/index.ts";
55

6-
export const extendRouter = (
7-
router: Express,
8-
simulationStore: ExtendedSimulationStore
9-
) => {
10-
router.get("/health", (_, response) => {
11-
response.send({ status: "ok" });
12-
});
13-
14-
router.use("/graphql", createHandler(simulationStore));
6+
export const extendRouter =
7+
(
8+
extend:
9+
| ((router: Express, simulationStore: ExtendedSimulationStore) => void)
10+
| undefined
11+
) =>
12+
(router: Express, simulationStore: ExtendedSimulationStore) => {
13+
if (extend) {
14+
extend(router, simulationStore);
15+
}
1516

16-
router.get(["/login/oauth/authorize"], (request, response) => {
17-
const { redirect_uri, state, env } = request.query as {
18-
[k: string]: string;
19-
};
20-
const code = "dev_code";
21-
const qs = stringify({
22-
code,
23-
env,
24-
state,
17+
router.get("/health", (_, response) => {
18+
response.send({ status: "ok" });
2519
});
2620

27-
const routerUrl = `${redirect_uri}?${qs}`;
28-
response.status(302).redirect(routerUrl);
29-
});
21+
router.use("/graphql", createHandler(simulationStore));
3022

31-
router.post(
32-
[
33-
"/login/oauth/access_token",
34-
"/api/v3/app/installations/:id/access_tokens",
35-
],
36-
(_request, response) => {
37-
// for /login/oauth/access_token
38-
const access_token = "dev_access_token";
39-
// for /app/installations/:id/access_tokens
40-
const token = "dev_token";
41-
const refresh_token = "dev_refresh_token";
42-
const repository_selection = "all";
43-
response.json({
44-
access_token,
45-
refresh_token,
46-
token,
47-
repository_selection,
23+
router.get(["/login/oauth/authorize"], (request, response) => {
24+
const { redirect_uri, state, env } = request.query as {
25+
[k: string]: string;
26+
};
27+
const code = "dev_code";
28+
const qs = stringify({
29+
code,
30+
env,
31+
state,
4832
});
49-
response.status(200);
50-
response.end();
51-
}
52-
);
53-
};
33+
34+
const routerUrl = `${redirect_uri}?${qs}`;
35+
response.status(302).redirect(routerUrl);
36+
});
37+
38+
router.post(
39+
[
40+
"/login/oauth/access_token",
41+
"/api/v3/app/installations/:id/access_tokens",
42+
],
43+
(_request, response) => {
44+
// for /login/oauth/access_token
45+
const access_token = "dev_access_token";
46+
// for /app/installations/:id/access_tokens
47+
const token = "dev_token";
48+
const refresh_token = "dev_refresh_token";
49+
const repository_selection = "all";
50+
response.json({
51+
access_token,
52+
refresh_token,
53+
token,
54+
repository_selection,
55+
});
56+
response.status(200);
57+
response.end();
58+
}
59+
);
60+
};

packages/github-api/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const simulation = (
8080
port: 3300, // default port
8181
simulationContextPage: "/simulation",
8282
extendStore: extendStoreConfig,
83-
extendRouter,
83+
extendRouter: extendRouter(args?.extend?.extendRouter),
8484
openapi: openapi(
8585
parsedInitialState,
8686
args?.apiUrl ?? "/",

0 commit comments

Comments
 (0)