-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(geoprocessing): api-events wrapper
- Loading branch information
Showing
11 changed files
with
147 additions
and
6 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
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 |
---|---|---|
|
@@ -9,5 +9,8 @@ | |
"host": "marxan-redis", | ||
"port": "6379" | ||
} | ||
} | ||
}, | ||
"api": { | ||
"url": "http://localhost:3000" | ||
} | ||
} |
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,9 @@ | ||
import { HttpModule, Module } from '@nestjs/common'; | ||
import { ApiEventsService } from './api-events.service'; | ||
|
||
@Module({ | ||
imports: [HttpModule], | ||
providers: [ApiEventsService], | ||
exports: [ApiEventsService], | ||
}) | ||
export class ApiEventsModule {} |
42 changes: 42 additions & 0 deletions
42
geoprocessing/src/modules/api-events/api-events.service.ts
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,42 @@ | ||
import { HttpService, Injectable } from '@nestjs/common'; | ||
import { API_EVENT_KINDS } from './events.enum'; | ||
import { AppConfig } from '../../utils/config.utils'; | ||
|
||
@Injectable() | ||
export class ApiEventsService { | ||
readonly #secret: string; | ||
readonly #apiUrl: string; | ||
|
||
constructor(private readonly http: HttpService) { | ||
// TODO debt: config shall be injected (nestjs/config); it isn't really unit-testable | ||
// will throw if not provided | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.#secret = AppConfig.get<string>('auth.xApiKey.secret')!; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
this.#apiUrl = AppConfig.get<string>('api.url')!; | ||
} | ||
|
||
async create<T>( | ||
resourceId: string, | ||
kind: API_EVENT_KINDS, | ||
data?: T, | ||
): Promise<void> { | ||
// TODO what if it failed? (currently validateStatus "swallows" the error) | ||
await this.http | ||
.post( | ||
this.#apiUrl + `/v1/api-events`, | ||
{ | ||
kind, | ||
topic: resourceId, | ||
data, | ||
}, | ||
{ | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
validateStatus: () => true, | ||
}, | ||
) | ||
.toPromise(); | ||
} | ||
} |
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,13 @@ | ||
/** | ||
* duplicated | ||
* @file api/src/modules/api-events/api-event.topic+kind.api.entity.ts | ||
*/ | ||
export enum API_EVENT_KINDS { | ||
user__signedUp__v1alpha1 = 'user.signedUp/v1alpha1', | ||
user__accountActivationTokenGenerated__v1alpha1 = 'user.accountActivationTokenGenerated/v1alpha1', | ||
user__accountActivationSucceeded__v1alpha1 = 'user.accountActivationSucceeded/v1alpha1', | ||
user__accountActivationFailed__v1alpha1 = 'user.accountActivationFailed/v1alpha1', | ||
user__passwordResetTokenGenerated__v1alpha1 = 'user.passwordResetTokenGenerated/v1alpha1', | ||
user__passwordResetSucceeded__v1alpha1 = 'user.passwordResetSucceeded/v1alpha1', | ||
user__passwordResetFailed__v1alpha1 = 'user.passwordResetFailed/v1alpha1', | ||
} |
51 changes: 51 additions & 0 deletions
51
geoprocessing/test/api-events.service.integration.e2e-spec.ts
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,51 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import AxiosMockAdapter from 'axios-mock-adapter'; | ||
import Axios from 'axios'; | ||
import { HttpService } from '@nestjs/common'; | ||
import { ApiEventsService } from '../src/modules/api-events/api-events.service'; | ||
import { API_EVENT_KINDS } from '../src/modules/api-events/events.enum'; | ||
|
||
let sut: ApiEventsService; | ||
let axiosMock: AxiosMockAdapter; | ||
|
||
const axios = Axios.create(); | ||
|
||
beforeEach(async () => { | ||
axiosMock = new AxiosMockAdapter(axios, { | ||
onNoMatch: 'throwException', | ||
}); | ||
const sandbox = await Test.createTestingModule({ | ||
providers: [ | ||
{ | ||
provide: `AXIOS_INSTANCE_TOKEN`, | ||
useValue: axios, | ||
}, | ||
HttpService, | ||
ApiEventsService, | ||
], | ||
}).compile(); | ||
sut = await sandbox.get(ApiEventsService); | ||
}); | ||
|
||
describe(`when creating an event succeed`, () => { | ||
const resourceId = `resource-id`; | ||
const data = { | ||
payload: 'value', | ||
} as const; | ||
const kind = API_EVENT_KINDS.user__accountActivationTokenGenerated__v1alpha1; | ||
|
||
beforeEach(() => { | ||
axiosMock | ||
.onPost('http://localhost:3030/v1/api-events', { | ||
kind: 'user.accountActivationTokenGenerated/v1alpha1', | ||
topic: resourceId, | ||
data, | ||
}) | ||
.replyOnce(201, {}); | ||
}); | ||
|
||
it(`should submit the required data`, async () => { | ||
await sut.create(resourceId, kind, data); | ||
expect(axiosMock.history.post.length).toEqual(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