-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* config and tests * use global config * simplify * docs * throw on defaultSuccessStatus is invalid
- Loading branch information
Showing
12 changed files
with
155 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
title: Config | ||
description: How to configure your oRPC project. | ||
--- | ||
|
||
```ts twoslash | ||
import { configGlobal } from '@orpc/contract'; // or '@orpc/server' | ||
|
||
configGlobal({ | ||
defaultMethod: 'GET', // Default HTTP method for requests | ||
defaultSuccessStatus: 200, // Default HTTP status for successful responses | ||
defaultInputStructure: 'compact', // Input payload structure: 'compact' or 'expanded' | ||
defaultOutputStructure: 'compact', // Output payload structure: 'compact' or 'expanded' | ||
}); | ||
``` | ||
|
||
I recommend placing this script at the top of the `main.ts` file, where the server is initialized. | ||
Alternatively, include it at the top of the file where you define `global oRPC builders or the app router`. | ||
This ensures the configuration is applied before any other part of your code is executed. |
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,19 @@ | ||
import { configGlobal, fallbackToGlobalConfig } from './config' | ||
|
||
it('configGlobal & fallbackToGlobalConfig', () => { | ||
configGlobal({ | ||
defaultMethod: 'GET', | ||
}) | ||
|
||
configGlobal({ | ||
// @ts-expect-error -- invalid value | ||
defaultMethod: 'INVALID', | ||
}) | ||
|
||
fallbackToGlobalConfig('defaultMethod', undefined) | ||
fallbackToGlobalConfig('defaultMethod', 'GET') | ||
// @ts-expect-error -- invalid value | ||
fallbackToGlobalConfig('defaultMethod', 'INVALID') | ||
// @ts-expect-error -- invalid global config | ||
fallbackToGlobalConfig('INVALID', 'GET') | ||
}) |
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,25 @@ | ||
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, defaultSuccessStatus: undefined }) | ||
|
||
expect(fallbackToGlobalConfig('defaultMethod', undefined)).toBe('POST') | ||
expect(fallbackToGlobalConfig('defaultSuccessStatus', undefined)).toBe(200) | ||
|
||
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: undefined, defaultSuccessStatus: undefined }) | ||
}) |
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,66 @@ | ||
import type { HTTPMethod, InputStructure } from './types' | ||
|
||
export interface ORPCConfig { | ||
/** | ||
* @default 'POST' | ||
*/ | ||
defaultMethod?: HTTPMethod | ||
|
||
/** | ||
* | ||
* @default 200 | ||
*/ | ||
defaultSuccessStatus?: number | ||
|
||
/** | ||
* | ||
* @default 'compact' | ||
*/ | ||
defaultInputStructure?: InputStructure | ||
|
||
/** | ||
* | ||
* @default 'compact' | ||
*/ | ||
defaultOutputStructure?: InputStructure | ||
} | ||
|
||
const DEFAULT_CONFIG: Required<ORPCConfig> = { | ||
defaultMethod: 'POST', | ||
defaultSuccessStatus: 200, | ||
defaultInputStructure: 'compact', | ||
defaultOutputStructure: 'compact', | ||
} | ||
|
||
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 | ||
} | ||
|
||
/** | ||
* Fallback the value to the global config if it is undefined | ||
*/ | ||
export function fallbackToGlobalConfig<T extends keyof ORPCConfig>(key: T, value: ORPCConfig[T]): Exclude<ORPCConfig[T], undefined> { | ||
if (value === undefined) { | ||
const fallback = GLOBAL_CONFIG_REF.value[key] | ||
|
||
if (fallback === undefined) { | ||
return DEFAULT_CONFIG[key] as any | ||
} | ||
|
||
return fallback as any | ||
} | ||
|
||
return value as any | ||
} |
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
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