This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
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.
- Loading branch information
Showing
14 changed files
with
264 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
import { Controller, Get, Param } from '@nestjs/common'; | ||
import { EffectiveUsersService } from './effective-users.service.ts'; | ||
|
||
@Controller('effective/users') | ||
export class EffectiveUsersController { | ||
constructor(private readonly effectiveUsersService: EffectiveUsersService) {} | ||
|
||
@Get(':id') | ||
findOne(@Param('id') id: number) { | ||
return this.effectiveUsersService.findOneWithLatestPosts(id); | ||
} | ||
} |
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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { EffectiveUsersController } from './effective-users.controller.ts'; | ||
import { EffectiveUsersService } from './effective-users.service.ts'; | ||
|
||
@Module({ | ||
controllers: [EffectiveUsersController], | ||
providers: [EffectiveUsersService], | ||
}) | ||
export class EffectiveUsersModule {} |
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,86 @@ | ||
import { FakeApiKiotaModule, type Post, type User } from '@app/fake-api-kiota'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { beforeEach, describe, it } from '@std/testing/bdd'; | ||
import { Effect } from 'effect'; | ||
import fetchMock from 'fetch-mock'; | ||
import createHttpError from 'http-errors'; | ||
import assert from 'node:assert'; | ||
import { EffectiveUsersService } from './effective-users.service.ts'; | ||
|
||
describe(EffectiveUsersService.name, () => { | ||
let service: EffectiveUsersService; | ||
|
||
describe(EffectiveUsersService.prototype.findOneWithLatestPosts.name, () => { | ||
const userId = 42; | ||
const user: User = { | ||
id: userId, | ||
name: 'Foo Bar', | ||
username: 'foobar', | ||
email: '[email protected]', | ||
}; | ||
const posts: Post[] = [{ | ||
id: 1, | ||
userId, | ||
title: 'post 1 title', | ||
body: 'post 1 body', | ||
}]; | ||
|
||
describe('when the user is not found', () => { | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
FakeApiKiotaModule.register({ | ||
customFetch: fetchMock | ||
.sandbox() | ||
.get(`path:/users/${userId}`, { body: {}, status: 404 }) | ||
.getOnce('path:/posts', posts, { query: { userId, limit: 5 } }), | ||
}), | ||
], | ||
providers: [EffectiveUsersService], | ||
}).compile(); | ||
|
||
service = module.get(EffectiveUsersService); | ||
}); | ||
|
||
it('should throw an error equivalent to HTTP 404 error', () => { | ||
assert.rejects( | ||
Effect.runPromise( | ||
service.findOneWithLatestPosts(userId).pipe( | ||
Effect.withConcurrency('unbounded'), | ||
), | ||
), | ||
createHttpError(404), | ||
); | ||
}); | ||
}); | ||
|
||
describe('when the user is found', () => { | ||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
FakeApiKiotaModule.register({ | ||
customFetch: fetchMock | ||
.sandbox() | ||
.get(`path:/users/${userId}`, user) | ||
.getOnce('path:/posts', posts, { query: { userId, limit: 5 } }), | ||
}), | ||
], | ||
providers: [EffectiveUsersService], | ||
}).compile(); | ||
|
||
service = module.get(EffectiveUsersService); | ||
}); | ||
|
||
it('should return a specified user with the latest posts', async () => { | ||
assert.deepStrictEqual( | ||
await Effect.runPromise(service.findOneWithLatestPosts(userId)), | ||
{ | ||
id: user.id, | ||
username: user.username, | ||
latestPosts: [{ id: posts[0].id, title: posts[0].title }], | ||
}, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
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,65 @@ | ||
import { | ||
FAKE_API_KIOTA_SERVICE_TOKEN, | ||
type FakeApiService, | ||
} from '@app/fake-api-kiota'; | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { Effect, identity, Match, Schedule } from 'effect'; | ||
import createError from 'http-errors'; | ||
|
||
@Injectable() | ||
export class EffectiveUsersService { | ||
constructor( | ||
@Inject(FAKE_API_KIOTA_SERVICE_TOKEN) private readonly apiService: | ||
FakeApiService, | ||
) {} | ||
|
||
findOneWithLatestPosts(id: number) { | ||
return Effect.all( | ||
[ | ||
Effect.retry( | ||
Effect.tryPromise(() => this.apiService.users.byUserId(id).get()), | ||
{ | ||
schedule: Schedule.exponential('100 millis'), | ||
times: 3, | ||
until: ({ error }) => | ||
Match.value(error).pipe( | ||
Match.when({ responseStatusCode: 404 }, () => true), | ||
Match.orElse(() => false), | ||
), | ||
}, | ||
), | ||
Effect.retry( | ||
Effect.tryPromise(() => | ||
this.apiService.posts.get({ | ||
queryParameters: { userId: id, limit: 5 }, | ||
}) | ||
), | ||
{ schedule: Schedule.exponential('100 millis'), times: 3 }, | ||
), | ||
], | ||
{ concurrency: 'unbounded' }, | ||
).pipe( | ||
Effect.flatMap(([user, posts]) => | ||
!user || !posts | ||
? Effect.die('Something went wrong!') | ||
: Effect.succeed([user, posts] as const) | ||
), | ||
Effect.map(([user, posts]) => ({ | ||
id: user.id, | ||
username: user.username, | ||
latestPosts: posts.map((post) => ({ id: post.id, title: post.title })), | ||
})), | ||
Effect.catchAll(({ error }) => | ||
Effect.fail( | ||
Match.value(error).pipe( | ||
Match.when( | ||
{ responseStatusCode: Match.number }, | ||
({ responseStatusCode }) => createError(responseStatusCode), | ||
), | ||
Match.orElse(identity), | ||
), | ||
) | ||
), | ||
); | ||
} | ||
} |
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