-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb71a1e
commit 547c8a9
Showing
8 changed files
with
405 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
"jsdom": "^21.1.2" | ||
}, | ||
"dependencies": { | ||
"@nestjs/config": "^3.2.2", | ||
"winston": "^3.10.0" | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,40 +1,56 @@ | ||
import { MongoClient } from "mongodb"; | ||
import { MongoDBOrderRepository } from "./adapters/order.repo"; | ||
import { MongoDBProductRepository } from "./adapters/product.repo"; | ||
import {MongoClient} from "mongodb"; | ||
import {MongoDBOrderRepository} from "./adapters/order.repo"; | ||
import {MongoDBProductRepository} from "./adapters/product.repo"; | ||
import {NestFactory} from "@nestjs/core"; | ||
import {AppModuleInversionOfControl} from "./app.module.ioc"; | ||
import {AppModuleOverrides} from "./app.module.overrides"; | ||
import {AppModuleWithRegister} from "./app.module.register"; | ||
import {MongoDBModule} from "./adapters/mongodb.module"; | ||
import {z} from "zod"; | ||
|
||
const EnvConfig = z.object({ | ||
MONGO_URI: z.string().default('mongodb://root:[email protected]'), | ||
MONGO_DB: z.string().default('store'), | ||
MONGO_CONNECT_TIMEOUT: z.number().default(100), | ||
MONGO_SOCKET_TIMEOUT: z.number().default(100), | ||
MONGO_SERVER_SELECTION_TIMEOUT: z.number().default(100), | ||
}).transform((input) => ({ | ||
uri: input.MONGO_URI, | ||
dbName: input.MONGO_DB, | ||
connectTimeoutMS: input.MONGO_CONNECT_TIMEOUT, | ||
socketTimeoutMS: input.MONGO_SOCKET_TIMEOUT, | ||
serverSelectionTimeoutMS: input.MONGO_SERVER_SELECTION_TIMEOUT, | ||
})); | ||
|
||
// @ts-ignore | ||
async function startServerIoC() { | ||
const mongo = await new MongoClient( | ||
`mongodb://root:[email protected]?retryWrites=true&writeConcern=majority` | ||
).connect(); | ||
const mongo = await new MongoClient( | ||
`mongodb://root:[email protected]?retryWrites=true&writeConcern=majority` | ||
).connect(); | ||
|
||
const db = mongo.db("store"); | ||
const productRepo = new MongoDBProductRepository(db); | ||
const orderRepo = new MongoDBOrderRepository(db); | ||
const db = mongo.db("store"); | ||
const productRepo = new MongoDBProductRepository(db); | ||
const orderRepo = new MongoDBOrderRepository(db); | ||
|
||
const app = await NestFactory.create(AppModuleInversionOfControl.register(productRepo, orderRepo)) | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
const app = await NestFactory.create(AppModuleInversionOfControl.register(productRepo, orderRepo)) | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
} | ||
|
||
// @ts-ignore | ||
async function startServerOverrides() { | ||
const app = await NestFactory.create(AppModuleOverrides) | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
const app = await NestFactory.create(AppModuleOverrides) | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
} | ||
|
||
async function startServerRegister() { | ||
const app = await NestFactory.create(AppModuleWithRegister.register( | ||
MongoDBModule.forRoot(`mongodb://root:[email protected]`) | ||
)); | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
const config = EnvConfig.parse(process.env); | ||
const app = await NestFactory.create(AppModuleWithRegister.register( | ||
MongoDBModule.forRoot(config) | ||
)); | ||
app.enableCors({origin: "*"}); | ||
await app.listen(8080); | ||
} | ||
|
||
void startServerRegister(); | ||
|
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,15 @@ | ||
import {ArgumentMetadata, BadRequestException, PipeTransform} from "@nestjs/common"; | ||
import {ZodSchema} from "zod"; | ||
|
||
export class ZodValidationPipe implements PipeTransform { | ||
constructor(private schema: ZodSchema) { | ||
} | ||
|
||
transform(value: unknown, {type}: ArgumentMetadata) { | ||
try { | ||
return this.schema.parse(value); | ||
} catch (error) { | ||
throw new BadRequestException(`failed parsing value ${value} into type ${type} with ${error}`); | ||
} | ||
} | ||
} |
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,46 @@ | ||
import {createTestingModule} from "../src/server.testkit"; | ||
import {aProduct} from "../src/builders"; | ||
import request from 'supertest'; | ||
|
||
async function createTestHarness() { | ||
const {nest, ...rest} = await createTestingModule(); | ||
return { | ||
app: request(nest.getHttpServer()), | ||
...rest | ||
} | ||
} | ||
|
||
// this test is not really required, it's wholly contained within purchase.flow.spec.tsx | ||
test('a user can order a product', async () => { | ||
const {app, productRepo, orderRepo} = await createTestHarness(); | ||
|
||
const moogOne = await productRepo.create(aProduct({title: "Moog One"})); | ||
const cartId = '666'; | ||
|
||
await app | ||
.post(`/cart/${cartId}`) | ||
.send({productId: moogOne.id}) | ||
.expect(201); | ||
|
||
await app | ||
.get(`/cart/${cartId}`) | ||
.expect({id: cartId, items: [{ | ||
productId: moogOne.id, | ||
price: moogOne.price, | ||
name: moogOne.title | ||
}]}); | ||
|
||
const orderId = await app | ||
.post(`/checkout/${666}`) | ||
.expect(201) | ||
.then(response => response.text); | ||
|
||
expect(orderRepo.orders).toContainEqual(expect.objectContaining({ | ||
id: orderId, | ||
items: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
productId: moogOne.id, | ||
}) | ||
]) | ||
})); | ||
}); |
Oops, something went wrong.