Skip to content

Commit

Permalink
feat(defaults): defaults are now accessible via `waychaser.currentDef…
Browse files Browse the repository at this point in the history
…aults`
  • Loading branch information
tompahoward committed Jun 26, 2022
1 parent d69eb8c commit 987d73b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/test/operations.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ export class OperationSteps {
})
};

@given('an endpoint returning {int}')
public async anEndpointReturningStatus(status: number) {
this.previousPath = this.currentPath
this.currentPath = randomApiPath()
this.router.get(this.currentPath, async (_request, response) => {
response.status(status).send();
})
};


@given('an endpoint with a self operation returning')
public async anEndpointWithASelfOperationReturning(documentString: string) {
this.previousPath = this.currentPath
Expand All @@ -110,6 +120,8 @@ export class OperationSteps {
})
};



@given('an endpoint at {string}')
public anEndpointAt(path: string) {
this.currentPath = path
Expand Down Expand Up @@ -894,6 +906,19 @@ export class OperationSteps {
})
};

@given('waychaser has a custom parser for 404s')
public async waychaserHasACustomParserFor404s() {
const existingContentParser = this.waychaser.currentDefaults.contentParser;
this.waychaser = this.waychaser.defaults({
contentParser: async (response: Response) => {
return response.status === 404 ? new ProblemDocument({
'title': "Not found",
'type': 'https://waychaser.io/not-found'
}) : existingContentParser(response);
}
})
};

@given('waychaser has a custom body handler')
public waychaserHasACustomBodyHandler() {
this.waychaser = this.waychaser.defaults({
Expand Down Expand Up @@ -1190,6 +1215,7 @@ export class OperationSteps {

@then('it will NOT have loaded successfully')
public itWillNOTHaveLoadedSuccessfully() {
console.log({ error: this.error })
if (this.response && this.response instanceof WayChaserResponse) {
expect(this.response?.ok).to.be.false
}
Expand All @@ -1198,6 +1224,11 @@ export class OperationSteps {
}
};

@then('it will NOT be a validation error')
public async itWillNOTBeAValidationError() {
expect(this.error.problem.type).to.not.equal('https://waychaser.io/validation-error')
};

@when('waychaser loads an endpoint that\'s not available')
public async waychaserLoadsAnEndpointThatIsNotAvailable() {
try {
Expand Down
13 changes: 11 additions & 2 deletions src/test/validator.feature
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ Feature: Validator
| name | string |
Then it will have loaded successfully

@wip
Scenario: Validate with operations
Given an endpoint with a self operation returning
"""
Expand All @@ -72,4 +71,14 @@ Feature: Validator
And we invoke the "self" operation with a validator expecting
| username | string |
| name | string |
Then it will have loaded successfully
Then it will have loaded successfully

@wip
Scenario: 404 with Validate
Given an endpoint returning 404
And waychaser has a custom parser for 404s
When waychaser loads that endpoint with a validator expecting
| username | string |
| name | string |
Then it will NOT have loaded successfully
And it will NOT be a validation error
5 changes: 3 additions & 2 deletions src/waychaser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ export async function _waychaser<Content>(


try {
const response = await WayChaserResponse.create(baseResponse, content, defaultOptions, mergedOptions)
const response = WayChaserResponse.create(baseResponse, content, defaultOptions, mergedOptions)

stop = false
for (const interceptor of updateOptions.postInterceptors) {
Expand Down Expand Up @@ -557,7 +557,7 @@ function _defaults(
const defaultedWaychaser = <Content>(
uriOrRequest: string | Request,
options?: Partial<WayChaserOptions<Content>>) => _waychaser<Content>(uriOrRequest, mergedOptions, options)

defaultedWaychaser.currentDefaults = mergedOptions;
defaultedWaychaser.defaults = (newDefaults: Partial<WayChaserOptions>) =>
_defaults(newDefaults, mergedOptions)
return defaultedWaychaser
Expand All @@ -581,6 +581,7 @@ export const waychaser = Object.assign(
return _waychaser(uriOrRequest, wayChaserDefaults, options)
},
{
currentDefaults: wayChaserDefaults,
defaults: (newDefaults: Partial<WayChaserOptions>) =>
_defaults(newDefaults, wayChaserDefaults)
}
Expand Down

0 comments on commit 987d73b

Please sign in to comment.