Skip to content

Commit

Permalink
add idempotency test endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarco committed Dec 24, 2024
1 parent 6775535 commit 20fdedd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ generation:
auth:
oAuth2ClientCredentialsEnabled: false
oAuth2PasswordEnabled: false
tests:
generateNewTests: true
typescript:
version: 0.0.1-alpha.162
version: 0.0.1-alpha.172
additionalDependencies:
dependencies: {}
devDependencies: {}
Expand Down
20 changes: 18 additions & 2 deletions src/hooks/novu-custom-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@ export class NovuCustomHook
implements BeforeRequestHook, AfterSuccessHook
{
beforeRequest(_hookCtx: BeforeRequestContext, request: Request): Request {
this.addAuthHeader(request);
this.addIdempotencyHeader(request)
return request;
}
private addIdempotencyHeader(request: Request) {
const idempotencyKey = 'Idempotency-Key';
const keyValue = request.headers.get(idempotencyKey);
if (!keyValue || keyValue==='' ) {
request.headers.set(idempotencyKey, this.generateIdempotencyKey())
}
}
private generateIdempotencyKey(): string {
const timestamp = Date.now();
const randomString = Math.random().toString(36).substr(2, 9); // Generates a random alphanumeric string
return `${timestamp}-${randomString}`;
}
private addAuthHeader(request: Request) {
const authKey = 'authorization';
const hasAuthorization = request.headers.has(authKey);
const apiKeyPrefix = 'ApiKey';
if (hasAuthorization) {
const key = request.headers.get(authKey);

if (key && !key.includes(apiKeyPrefix)) {
request.headers.set(authKey,`${apiKeyPrefix} ${key}`)
request.headers.set(authKey, `${apiKeyPrefix} ${key}`)
}
}
return request;
}

async afterSuccess(_hookCtx: AfterSuccessContext, response: Response): Promise<Response> {
Expand Down

0 comments on commit 20fdedd

Please sign in to comment.