Skip to content

Commit

Permalink
foundation simulator handles json serving (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbolda authored Aug 12, 2024
1 parent 74f4cd3 commit f20a3a6
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/serve-json-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@simulacrum/foundation-simulator": minor:feat
---

We now support serving a directly of JSON files through file path routing. Use `serveJsonFiles` to specify the folder which contains the files to serve.
43 changes: 43 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/foundation/example/extensiveServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { extendRouter } from "./extend-api";

export const simulation = createFoundationSimulationServer({
port: 9999,
serveJsonFiles: `${__dirname}/jsonFiles`,
openapi,
extendStore,
extendRouter,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": "quite ok"
}
1 change: 1 addition & 0 deletions packages/foundation/example/singleFileServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const openapiSchemaWithModificationsForSimulation = {

export const simulation = createFoundationSimulationServer({
port: 9999,
serveJsonFiles: `${__dirname}/jsonFiles`,
openapi: [
{
document: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": "quite ok"
}
1 change: 1 addition & 0 deletions packages/foundation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"ajv-formats": "^3.0.1",
"express": "^4.19.2",
"fdir": "^6.2.0",
"openapi-backend": "^5.10.6",
"openapi-merge": "^1.3.2",
"starfx": "^0.12.0"
Expand Down
25 changes: 25 additions & 0 deletions packages/foundation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type {
Request as ExpressRequest,
Response as ExpressResponse,
} from "express";
import { fdir } from "fdir";
import fs from "node:fs";
import path from "node:path";
import { merge } from "lodash";
import OpenAPIBackend from "openapi-backend";
import type {
Expand Down Expand Up @@ -47,11 +50,13 @@ export function createFoundationSimulationServer<
ExtendedSimulationSelectors
>({
port = 9000,
serveJsonFiles,
openapi,
extendStore,
extendRouter,
}: {
port: number;
serveJsonFiles?: string;
openapi?: {
document: Document | (Document | RecursivePartial<Document>)[];
handlers: (
Expand Down Expand Up @@ -89,6 +94,26 @@ export function createFoundationSimulationServer<
app.use(express.json());
let simulationStore = createSimulationStore(extendStore);

if (serveJsonFiles) {
const jsonFiles = new fdir()
.filter((path, _isDirectory) => path.endsWith(".json"))
.withDirs()
.withRelativePaths()
.crawl(serveJsonFiles)
.sync();

if (jsonFiles.length > 0) {
for (let jsonFile of jsonFiles) {
const route = jsonFile.slice(0, jsonFile.length - 5);
const filename = path.join(serveJsonFiles, jsonFile);
app.get(`/${route}`, (_req, res) => {
res.setHeader("content-type", "application/json");
fs.createReadStream(filename).pipe(res);
});
}
}
}

if (extendRouter) {
extendRouter(app, simulationStore);
}
Expand Down

0 comments on commit f20a3a6

Please sign in to comment.