Skip to content

Commit

Permalink
chore: Introduce type for plugin modules
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyJones committed Jun 10, 2024
1 parent b14aa27 commit 4268532
Show file tree
Hide file tree
Showing 24 changed files with 371 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ export const HTTP_REQUEST_MATCHER_TYPE = '_case:HttpRequestMatcher' as const;
export const HTTP_STATUS_CODE_MATCHER_TYPE = '_case:HttpStatusCode' as const;
export const URL_ENCODED_STRING_TYPE = '_case:UrlEncodedString' as const;
export const HTTP_BASIC_AUTH_TYPE = '_case:HttpBasicAuth' as const;

export type AllHttpMatcherTypes =
| typeof HTTP_RESPONSE_MATCHER_TYPE
| typeof HTTP_REQUEST_MATCHER_TYPE
| typeof HTTP_STATUS_CODE_MATCHER_TYPE
| typeof URL_ENCODED_STRING_TYPE
| typeof HTTP_BASIC_AUTH_TYPE;
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,10 @@ export interface CoreHttpResponseMatcher {
body?: AnyCaseMatcherOrData;
headers?: AnyCaseMatcherOrData | Record<string, AnyCaseMatcherOrData>;
}

export type AllHttpMatcherDescriptors =
| CoreUrlEncodedStringMatcher
| CoreHttpStatusCodeMatcher
| CoreHttpBasicAuthValueMatcher
| CoreHttpRequestMatcher
| CoreHttpResponseMatcher;
56 changes: 15 additions & 41 deletions packages/case-core-plugin-http-dsl/src/mocks/setup.types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
AnyCaseMatcherOrData,
MatchContext,
BaseSetupInfo,
HasTypeForMockDescriptor,
CaseMockDescriptorFor,
} from '@contract-case/case-plugin-base';
import {
MOCK_HTTP_SERVER,
Expand All @@ -11,52 +9,28 @@ import {
} from './constants.types';
import { ConsumeHttpResponse, ProduceHttpResponse } from './definitions.types';

type MockOutput = {
actual: unknown;
expected: AnyCaseMatcherOrData;
context: MatchContext;
};
export type AllHttpMockSetupInfo =
| HttpRequestConsumerSetup
| HttpRequestProducerSetup;

type AnySetupInfo = HttpRequestConsumerSetup | HttpRequestProducerSetup;

export type SetupInfoFor<T extends HttpMockDescriptorTypes> = Extract<
AnySetupInfo,
HasTypeForMockDescriptor<T>
> &
BaseConfig;

export type MockData<T extends HttpMockDescriptorTypes> = {
config: SetupInfoFor<T>;
assertableData: () => Promise<MockOutput>;
};

export type MockSetupFn<T extends HttpMockDescriptorTypes> = (
mock: CaseMockDescriptorFor<ConsumeHttpResponse | ProduceHttpResponse, T>,
context: MatchContext,
) => Promise<MockData<T>>;

type BaseConfig = {
// We allow Any here for now, because it makes defining tests very convenient
// TODO: Don't use any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
variables: Record<string, any>;
};
export type AllHttpMockExecutors = ConsumeHttpResponse | ProduceHttpResponse;

export type HttpRequestConsumerSetup = HasTypeForMockDescriptor<
typeof MOCK_HTTP_SERVER
> &
BaseConfig & {
BaseSetupInfo & {
baseUrl: string;
};

export type HttpRequestProducerSetup = HasTypeForMockDescriptor<
typeof MOCK_HTTP_CLIENT
> &
BaseConfig;

export type ArbitraryConfig<T extends HttpMockDescriptorTypes> = BaseConfig & {
'_case:mock:type': T;
// TODO don't use any here
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[k: string]: any;
};
BaseSetupInfo;

export type ArbitraryConfig<T extends HttpMockDescriptorTypes> =
BaseSetupInfo & {
'_case:mock:type': T;
// TODO don't use any here
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[k: string]: any;
};
43 changes: 43 additions & 0 deletions packages/case-core-plugin-http/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,49 @@
* BSD-3-Clause license
*/

import {
AllHttpMatcherDescriptors,
AllHttpMatcherTypes,
AllHttpMockExecutors,
AllHttpMockSetupInfo,
HTTP_BASIC_AUTH_TYPE,
HTTP_REQUEST_MATCHER_TYPE,
HTTP_RESPONSE_MATCHER_TYPE,
HTTP_STATUS_CODE_MATCHER_TYPE,
MOCK_HTTP_CLIENT,
MOCK_HTTP_SERVER,
URL_ENCODED_STRING_TYPE,
} from '@contract-case/case-core-plugin-http-dsl';
import { ContractCasePlugin } from '@contract-case/case-plugin-base';
import {
HttpBasicAuthMatcher,
HttpRequestMatcher,
HttpResponseMatcher,
HttpStatusCodeMatcher,
UrlEncodedStringMatcher,
} from './matchers';
import { setupHttpResponseConsumer, setupHttpResponseProducer } from './mocks';

export * from './matchers';
export * from './mocks';
export * from './mocks/types';

export const CoreHttpPlugin: ContractCasePlugin<
AllHttpMatcherTypes,
typeof MOCK_HTTP_CLIENT | typeof MOCK_HTTP_SERVER,
AllHttpMatcherDescriptors,
AllHttpMockExecutors,
AllHttpMockSetupInfo
> = {
matcherExecutors: {
[HTTP_BASIC_AUTH_TYPE]: HttpBasicAuthMatcher,
[HTTP_REQUEST_MATCHER_TYPE]: HttpRequestMatcher,
[HTTP_RESPONSE_MATCHER_TYPE]: HttpResponseMatcher,
[HTTP_STATUS_CODE_MATCHER_TYPE]: HttpStatusCodeMatcher,
[URL_ENCODED_STRING_TYPE]: UrlEncodedStringMatcher,
},
setupMocks: {
[MOCK_HTTP_CLIENT]: setupHttpResponseConsumer,
[MOCK_HTTP_SERVER]: setupHttpResponseProducer,
},
};
5 changes: 3 additions & 2 deletions packages/case-core-plugin-http/src/mocks/mockHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
DataContext,
HasBaseUrlUnderTest,
MatchContext,
MockData,
addLocation,
} from '@contract-case/case-plugin-base';
import {
HttpRequestData,
CoreHttpRequestResponseMatcherPair,
MockData,
MOCK_HTTP_CLIENT,
AllHttpMockSetupInfo,
} from '@contract-case/case-core-plugin-http-dsl';

import { makeAssertionsOn } from './assert/assert';
Expand Down Expand Up @@ -72,7 +73,7 @@ export const setupHttpResponseConsumer = (
response: expectedResponse,
}: CoreHttpRequestResponseMatcherPair,
parentContext: MatchContext,
): Promise<MockData<typeof MOCK_HTTP_CLIENT>> =>
): Promise<MockData<AllHttpMockSetupInfo, typeof MOCK_HTTP_CLIENT>> =>
Promise.resolve().then(() => {
const expectedRequest = validateHttpRequestData(
parentContext.descendAndStrip(
Expand Down
Loading

0 comments on commit 4268532

Please sign in to comment.