-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add isomorphic content API utilities
- Loading branch information
1 parent
c647d97
commit 5f61e7c
Showing
7 changed files
with
392 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 @@ | ||
import {Evaluator, EvaluationOptions as BaseOptions} from '@croct/sdk/evaluator'; | ||
import {JsonValue} from '../sdk/json'; | ||
|
||
export type EvaluationOptions<T extends JsonValue = JsonValue> = BaseOptions & AuthOptions & FetchingOptions<T>; | ||
|
||
type FetchingOptions<T extends JsonValue> = { | ||
baseEndpointUrl?: string, | ||
fallback?: T, | ||
}; | ||
|
||
type AuthOptions = ServerSideAuthOptions | ClientSideAuthOptions; | ||
|
||
type ServerSideAuthOptions = { | ||
apiKey: string, | ||
appId?: never, | ||
}; | ||
|
||
type ClientSideAuthOptions = { | ||
appId: string, | ||
apiKey?: never, | ||
}; | ||
|
||
export function evaluate<T extends JsonValue>(query: string, options: EvaluationOptions<T>): Promise<T> { | ||
const {baseEndpointUrl, fallback, apiKey, appId, ...evaluation} = options; | ||
const auth: AuthOptions = apiKey !== undefined ? {apiKey: apiKey} : {appId: appId}; | ||
const promise = (new Evaluator({...auth, baseEndpointUrl: baseEndpointUrl})) | ||
.evaluate(query, evaluation) as Promise<T>; | ||
|
||
if (fallback !== undefined) { | ||
return promise.catch(() => fallback); | ||
} | ||
|
||
return promise; | ||
} |
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,60 @@ | ||
import { | ||
ContentFetcher, | ||
DynamicContentOptions as BaseDynamicOptions, | ||
StaticContentOptions as BaseStaticOptions, | ||
} from '@croct/sdk/contentFetcher'; | ||
import {JsonObject, JsonValue} from '../sdk/json'; | ||
import {FetchResponse} from '../plug'; | ||
import {SlotContent, VersionedSlotId} from '../slot'; | ||
|
||
type FetchingOptions<T extends JsonValue> = { | ||
baseEndpointUrl?: string, | ||
fallback?: T, | ||
}; | ||
|
||
type AuthOptions = ServerSideAuthOptions | ClientSideAuthOptions; | ||
|
||
type ServerSideAuthOptions = { | ||
apiKey: string, | ||
appId?: never, | ||
}; | ||
|
||
type ClientSideAuthOptions = { | ||
appId: string, | ||
apiKey?: never, | ||
}; | ||
|
||
export type DynamicContentOptions<T extends JsonObject = JsonObject> = | ||
Omit<BaseDynamicOptions, 'version'> & FetchingOptions<T> & AuthOptions; | ||
|
||
export type StaticContentOptions<T extends JsonObject = JsonObject> = | ||
Omit<BaseStaticOptions, 'version'> & FetchingOptions<T> & ServerSideAuthOptions; | ||
|
||
export type FetchOptions<T extends JsonObject = SlotContent> = DynamicContentOptions<T> | StaticContentOptions<T>; | ||
|
||
export function fetchContent<I extends VersionedSlotId, C extends JsonObject>( | ||
slotId: I, | ||
options?: FetchOptions<SlotContent<I, C>>, | ||
): Promise<Omit<FetchResponse<I, C>, 'payload'>> { | ||
const {apiKey, appId, fallback, baseEndpointUrl, ...fetchOptions} = options ?? {}; | ||
const auth = {appId: appId, apiKey: apiKey}; | ||
const [id, version = 'latest'] = slotId.split('@') as [I, `${number}` | 'latest' | undefined]; | ||
|
||
const promise = (new ContentFetcher({...auth, baseEndpointUrl: baseEndpointUrl})) | ||
.fetch<SlotContent<I, C>>( | ||
id, | ||
version === 'latest' | ||
? fetchOptions | ||
: {...fetchOptions, version: version}, | ||
); | ||
|
||
if (fallback !== undefined) { | ||
return promise.catch( | ||
() => ({ | ||
content: fallback, | ||
}), | ||
); | ||
} | ||
|
||
return promise; | ||
} |
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,2 @@ | ||
export * from './evaluate'; | ||
export * from './fetchContent'; |
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,85 @@ | ||
import {Evaluator} from '@croct/sdk/evaluator'; | ||
import {evaluate, EvaluationOptions} from '../../src/api'; | ||
|
||
const mockEvaluate: Evaluator['evaluate'] = jest.fn(); | ||
|
||
jest.mock( | ||
'@croct/sdk/evaluator', | ||
() => ({ | ||
__esModule: true, | ||
/* | ||
* eslint-disable-next-line prefer-arrow-callback -- | ||
* The mock can't be an arrow function because calling new on | ||
* an arrow function is not allowed in JavaScript. | ||
*/ | ||
Evaluator: jest.fn(function constructor(this: Evaluator) { | ||
this.evaluate = mockEvaluate; | ||
}), | ||
}), | ||
); | ||
|
||
describe('evaluate', () => { | ||
const apiKey = '00000000-0000-0000-0000-000000000000'; | ||
const appId = '00000000-0000-0000-0000-000000000000'; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should forward a server-side evaluation request', async () => { | ||
const options: EvaluationOptions = { | ||
apiKey: apiKey, | ||
timeout: 100, | ||
baseEndpointUrl: 'https://croct.example.com', | ||
}; | ||
|
||
const query = 'true'; | ||
|
||
jest.mocked(mockEvaluate).mockResolvedValue(true); | ||
|
||
await expect(evaluate(query, options)).resolves.toBe(true); | ||
|
||
expect(Evaluator).toHaveBeenCalledWith({ | ||
apiKey: options.apiKey, | ||
baseEndpointUrl: options.baseEndpointUrl, | ||
}); | ||
|
||
expect(mockEvaluate).toHaveBeenCalledWith(query, { | ||
timeout: options.timeout, | ||
}); | ||
}); | ||
|
||
it('should forward a client-side evaluation request', async () => { | ||
const options: EvaluationOptions = { | ||
appId: appId, | ||
timeout: 100, | ||
baseEndpointUrl: 'https://croct.example.com', | ||
}; | ||
|
||
const query = 'true'; | ||
|
||
jest.mocked(mockEvaluate).mockResolvedValue(true); | ||
|
||
await expect(evaluate(query, options)).resolves.toBe(true); | ||
|
||
expect(Evaluator).toHaveBeenCalledWith({ | ||
appId: options.appId, | ||
baseEndpointUrl: options.baseEndpointUrl, | ||
}); | ||
|
||
expect(mockEvaluate).toHaveBeenCalledWith(query, { | ||
timeout: options.timeout, | ||
}); | ||
}); | ||
|
||
it('should return the fallback value on error', async () => { | ||
const options: EvaluationOptions = { | ||
apiKey: apiKey, | ||
fallback: false, | ||
}; | ||
|
||
jest.mocked(mockEvaluate).mockRejectedValue(new Error('error')); | ||
|
||
await expect(evaluate('true', options)).resolves.toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.