Skip to content

Commit

Permalink
throw on defaultSuccessStatus is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
unnoq committed Jan 3, 2025
1 parent 955f595 commit c511154
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
14 changes: 7 additions & 7 deletions packages/contract/src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { configGlobal, fallbackToGlobalConfig } from './config'
it('configGlobal & fallbackToGlobalConfig', () => {
configGlobal({
defaultMethod: 'GET',
defaultSuccessStatus: 203,
})

expect(fallbackToGlobalConfig('defaultMethod', undefined)).toBe('GET')
expect(fallbackToGlobalConfig('defaultSuccessStatus', undefined)).toBe(203)

configGlobal({
defaultMethod: undefined,
})
configGlobal({ defaultMethod: undefined, defaultSuccessStatus: undefined })

expect(fallbackToGlobalConfig('defaultMethod', undefined)).toBe('POST')
expect(fallbackToGlobalConfig('defaultSuccessStatus', undefined)).toBe(200)

expect(fallbackToGlobalConfig('defaultMethod', 'DELETE')).toBe('DELETE')
expect(() => configGlobal({ defaultSuccessStatus: 300 })).toThrowError()

expect(fallbackToGlobalConfig('defaultMethod', 'DELETE')).toBe('DELETE')
expect(fallbackToGlobalConfig('defaultInputStructure', undefined)).toBe('compact')
expect(fallbackToGlobalConfig('defaultInputStructure', 'detailed')).toBe('detailed')

/** Reset to make sure the global config is not affected other tests */
configGlobal({
defaultMethod: 'POST',
})
configGlobal({ defaultMethod: undefined, defaultSuccessStatus: undefined })
})
7 changes: 7 additions & 0 deletions packages/contract/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ const GLOBAL_CONFIG_REF: { value: ORPCConfig } = { value: DEFAULT_CONFIG }
* Set the global configuration, this configuration can effect entire project
*/
export function configGlobal(config: ORPCConfig): void {
if (
config.defaultSuccessStatus !== undefined
&& (config.defaultSuccessStatus < 200 || config.defaultSuccessStatus > 299)
) {
throw new Error('[configGlobal] The defaultSuccessStatus must be between 200 and 299')
}

GLOBAL_CONFIG_REF.value = config
}

Expand Down

0 comments on commit c511154

Please sign in to comment.