Skip to content

Commit

Permalink
refactor(#zimic): @zimic/http package (#570)
Browse files Browse the repository at this point in the history
### Refactoring
- Created the package `@zimic/http` to contain resources related to
HTTP. It is a peer dependency of `zimic` and should be installed
alongside it.
  
  With this change, the following exports were affected:
  - `zimic` -> `@zimic/http`
  - `zimic/http` -> `@zimic/http`
  - `zimic/typegen` -> `@zimic/http/typegen`

The errors `InvalidJSONError` and `InvalidFormDataError`, previously
exported by `zimic` and `zimic/http`, are not exported by
`zimic/interceptor/http`;

The Zimic typegen CLI was also migrated to `@zimic/http` and can now be
called with `zimic-http typegen openapi ...`.

This PR starts migrating the documentation to consider `@zimic/http`.
Further PRs will finish the migration.

Part of #565.
  • Loading branch information
diego-aquino authored Feb 19, 2025
1 parent 68a4209 commit 14fc235
Show file tree
Hide file tree
Showing 244 changed files with 1,515 additions and 632 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Zimic provides a flexible and type-safe way to mock HTTP requests.
[getting started guide](https://github.com/zimicjs/zimic/wiki/getting‐started) and starting mocking!

```ts
import { type HttpSchema } from 'zimic/http';
import { type HttpSchema } from '@zimic/http';
import { httpInterceptor } from 'zimic/interceptor/http';

// 1. Declare your types:
Expand Down
3 changes: 2 additions & 1 deletion apps/zimic-test-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": false,
"scripts": {
"lint": "eslint --cache --no-error-on-unmatched-pattern --fix",
"lint": "eslint --cache --no-error-on-unmatched-pattern --no-warn-ignored --fix",
"lint:turbo": "pnpm lint . --max-warnings 0",
"style": "prettier --log-level warn --ignore-unknown --no-error-on-unmatched-pattern --cache",
"style:check": "pnpm style --check",
Expand All @@ -20,6 +20,7 @@
"@vitest/browser": "^3.0.5",
"@vitest/coverage-istanbul": "^3.0.5",
"@zimic/eslint-config-node": "workspace:*",
"@zimic/http": "workspace:*",
"@zimic/lint-staged-config": "workspace:*",
"@zimic/tsconfig": "workspace:*",
"axios": "^1.7.9",
Expand Down
2 changes: 1 addition & 1 deletion apps/zimic-test-client/tests/exports/exports.node.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { typegen, type TypegenNamespace, type OpenAPITypegenOptions } from '@zimic/http/typegen';
import { describe, expect, expectTypeOf, it } from 'vitest';
import {
interceptorServer,
Expand All @@ -8,7 +9,6 @@ import {
DEFAULT_ACCESS_CONTROL_HEADERS,
DEFAULT_PREFLIGHT_STATUS_CODE,
} from 'zimic/interceptor/server';
import { typegen, type TypegenNamespace, type OpenAPITypegenOptions } from 'zimic/typegen';

describe('Exports (Node.js)', () => {
it('should export all expected resources', () => {
Expand Down
36 changes: 19 additions & 17 deletions apps/zimic-test-client/tests/exports/exports.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import { describe, expect, expectTypeOf, it } from 'vitest';
import { type JSONValue, type JSONSerialized, InvalidJSONError } from 'zimic';
import {
createFetch,
type Fetch,
type FetchClient,
type FetchClientOptions,
type FetchFunction,
type FetchInput,
type FetchRequest,
type FetchRequestConstructor,
type FetchRequestInit,
type FetchResponse,
FetchResponseError,
} from 'zimic/fetch';
import {
type JSONValue,
type JSONSerialized,
type HttpBody,
type HttpRequest,
type HttpResponse,
Expand Down Expand Up @@ -51,8 +38,21 @@ import {
type HttpSchemaPath,
type InferPathParams,
type MergeHttpResponsesByStatusCode,
InvalidFormDataError,
} from 'zimic/http';
} from '@zimic/http';
import { describe, expect, expectTypeOf, it } from 'vitest';
import {
createFetch,
type Fetch,
type FetchClient,
type FetchClientOptions,
type FetchFunction,
type FetchInput,
type FetchRequest,
type FetchRequestConstructor,
type FetchRequestInit,
type FetchResponse,
FetchResponseError,
} from 'zimic/fetch';
import {
httpInterceptor,
type HttpInterceptorNamespace,
Expand Down Expand Up @@ -90,6 +90,8 @@ import {
UnregisteredBrowserServiceWorkerError,
DisabledRequestSavingError,
TimesCheckError,
InvalidJSONError,
InvalidFormDataError,
} from 'zimic/interceptor/http';

describe('Exports', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { JSONSerialized, HttpHeaders, HttpRequest, HttpResponse, HttpSchema, HttpSearchParams } from '@zimic/http';
import { beforeAll, beforeEach, afterAll, expect, describe, it, expectTypeOf, afterEach } from 'vitest';
import { JSONSerialized } from 'zimic';
import { HttpHeaders, HttpRequest, HttpResponse, HttpSchema, HttpSearchParams } from 'zimic/http';
import { httpInterceptor, HttpInterceptorType } from 'zimic/interceptor/http';

import { importCrypto, IsomorphicCrypto } from '@tests/utils/crypto';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { JSONValue, JSONSerialized } from 'zimic';
import { HttpSchema } from 'zimic/http';
import { JSONValue, JSONSerialized, HttpSchema } from '@zimic/http';

export interface User {
id: string;
Expand Down
2 changes: 1 addition & 1 deletion apps/zimic-test-client/tests/typegen/typegen.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('Typegen', () => {

await $(
'pnpm',
['zimic', 'typegen', 'openapi', input, '--output', generatedFilePath, '--service-name', serviceName],
['zimic-http', 'typegen', 'openapi', input, '--output', generatedFilePath, '--service-name', serviceName],
{ stderr: 'inherit' },
);

Expand Down
6 changes: 3 additions & 3 deletions docs/wiki/api‐zimic.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The module `zimic` exports general resources and utility types used by Zimic.
Represents or validates a type that is compatible with JSON.

```ts
import { type JSONValue } from 'zimic';
import { type JSONValue } from '@zimic/http';

// Can be used as a standalone type:
const value: JSONValue = {
Expand All @@ -37,7 +37,7 @@ const value: JSONValue = {
```

```ts
import { type JSONValue } from 'zimic';
import { type JSONValue } from '@zimic/http';

// Can be used with a type argument to validate a JSON value:
type ValidJSON = JSONValue<{
Expand Down Expand Up @@ -66,7 +66,7 @@ Recursively converts a type to its JSON-serialized version. Dates are converted
are excluded.

```ts
import { type JSONSerialized } from 'zimic';
import { type JSONSerialized } from '@zimic/http';

type SerializedUser = JSONSerialized<{
id: string;
Expand Down
34 changes: 17 additions & 17 deletions docs/wiki/api‐zimic‐http.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ schema. `HttpHeaders` is fully compatible with `Headers` and is used by Zimic to
headers.

```ts
import { HttpHeaders } from 'zimic/http';
import { HttpHeaders } from '@zimic/http';

const headers = new HttpHeaders<{
accept?: string;
Expand All @@ -58,7 +58,7 @@ console.log(contentType); // 'application/json'
other headers:

```ts
import { type HttpSchema, HttpHeaders } from 'zimic/http';
import { type HttpSchema, HttpHeaders } from '@zimic/http';

type HeaderSchema = HttpSchema.Headers<{
accept?: string;
Expand Down Expand Up @@ -102,7 +102,7 @@ version. Numbers and booleans are converted to `${number}` and `${boolean}` resp
serializable values are excluded, such as functions and dates.

```ts
import { type HttpSearchParamsSerialized } from 'zimic/http';
import { type HttpSearchParamsSerialized } from '@zimic/http';

type Params = HttpSearchParamsSerialized<{
'content-type': string;
Expand All @@ -123,7 +123,7 @@ type Params = HttpSearchParamsSerialized<{
Extracts the names of the headers defined in a `HttpHeadersSchema`. Each key is considered a header name.

```ts
import { type HttpSearchParamsSerialized } from 'zimic/http';
import { type HttpSearchParamsSerialized } from '@zimic/http';

type HeaderName = HttpHeadersSchemaName<{
'content-type': string;
Expand All @@ -139,7 +139,7 @@ strictly-typed schema. `HttpSearchParams` is fully compatible with `URLSearchPar
type safety when managing search parameters.

```ts
import { HttpSearchParams } from 'zimic/http';
import { HttpSearchParams } from '@zimic/http';

const searchParams = new HttpSearchParams<{
names?: string[];
Expand All @@ -162,7 +162,7 @@ console.log(page); // '1'
comparisons with other search params:

```ts
import { type HttpSchema, HttpSearchParams } from 'zimic/http';
import { type HttpSchema, HttpSearchParams } from '@zimic/http';

type SearchParamsSchema = HttpSchema.SearchParams<{
names?: string[];
Expand Down Expand Up @@ -207,7 +207,7 @@ are converted to `${number}` and `${boolean}` respectively, null becomes undefin
excluded, such as functions and dates.

```ts
import { type HttpSearchParamsSerialized } from 'zimic/http';
import { type HttpSearchParamsSerialized } from '@zimic/http';

type Params = HttpSearchParamsSerialized<{
query: string | null;
Expand All @@ -230,7 +230,7 @@ name. `HttpSearchParamsSchemaName.Array` can be used to extract the names of arr
`HttpSearchParamsSchemaName.NonArray` extracts the names of non-array search params.

```ts
import { type HttpSearchParamsSchemaName } from 'zimic/http';
import { type HttpSearchParamsSchemaName } from '@zimic/http';

type SearchParamsName = HttpSearchParamsSchemaName<{
query?: string[];
Expand Down Expand Up @@ -261,7 +261,7 @@ strictly-typed schema. `HttpFormData` is fully compatible with `FormData` and is
when managing form data.

```ts
import { HttpFormData } from 'zimic/http';
import { HttpFormData } from '@zimic/http';

const formData = new HttpFormData<{
files: File[];
Expand All @@ -284,7 +284,7 @@ console.log(description); // 'My file'
with other form data:

```ts
import { type HttpSchema, HttpFormData } from 'zimic/http';
import { type HttpSchema, HttpFormData } from '@zimic/http';

type FormDataSchema = HttpSchema.FormData<{
files: File[];
Expand Down Expand Up @@ -321,7 +321,7 @@ Numbers and booleans are converted to `${number}` and `${boolean}` respectively,
excluded, such as functions and dates.

```ts
import { type HttpFormDataSerialized } from 'zimic/http';
import { type HttpFormDataSerialized } from '@zimic/http';

type Schema = HttpFormDataSerialized<{
contentTitle: string | null;
Expand All @@ -348,7 +348,7 @@ schema. Optionally receives a second argument with one or more methods to filter
defined in the schema are allowed.

```ts
import { type HttpSchema, type HttpSchemaPath } from 'zimic/http';
import { type HttpSchema, type HttpSchemaPath } from '@zimic/http';

type Schema = HttpSchema<{
'/users': {
Expand Down Expand Up @@ -376,7 +376,7 @@ Extracts the literal paths from an HTTP service schema. Optionally receives a se
to filter the paths with. Only the methods defined in the schema are allowed.

```ts
import { type HttpSchema, type LiteralHttpSchemaPath } from 'zimic/http';
import { type HttpSchema, type LiteralHttpSchemaPath } from '@zimic/http';

type Schema = HttpSchema<{
'/users': {
Expand Down Expand Up @@ -404,7 +404,7 @@ Extracts the non-literal paths from an HTTP service schema. Optionally receives
methods to filter the paths with. Only the methods defined in the schema are allowed.

```ts
import { type HttpSchema, type NonLiteralHttpSchemaPath } from 'zimic/http';
import { type HttpSchema, type NonLiteralHttpSchemaPath } from '@zimic/http';

type Schema = HttpSchema<{
'/users': {
Expand Down Expand Up @@ -435,7 +435,7 @@ If the first argument is a [HttpSchema](api‐zimic‐interceptor‐http‐schem
checked to be a valid path in that schema.

```ts
import { HttpSchema, InferPathParams } from 'zimic/http';
import { HttpSchema, InferPathParams } from '@zimic/http';

type MySchema = HttpSchema<{
'/users/:userId': {
Expand All @@ -451,7 +451,7 @@ type PathParams = InferPathParams<MySchema, '/users/:userId'>;
```

```ts
import { InferPathParams } from 'zimic/http';
import { InferPathParams } from '@zimic/http';

// Without using a schema to validate the path (works as `PathParamsSchemaFromPath`):
type PathParams = InferPathParams<'/users/:userId'>;
Expand All @@ -464,7 +464,7 @@ Merges multiple HTTP response schemas by status code into a single schema. When
first declaration takes precedence.

```ts
import { type HttpSchema, type HttpStatusCode, type MergeHttpResponsesByStatusCode } from 'zimic/http';
import { type HttpSchema, type HttpStatusCode, type MergeHttpResponsesByStatusCode } from '@zimic/http';

// Overriding the 400 status code with a more specific schema
// and using a generic schema for all other client errors.
Expand Down
Loading

0 comments on commit 14fc235

Please sign in to comment.