Skip to content

Commit

Permalink
refactor: rename http status code type
Browse files Browse the repository at this point in the history
  • Loading branch information
joshamaju committed Jul 16, 2024
1 parent fd6589c commit a9b8769
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-ravens-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect-fetch": minor
---

Rename the Typescript type for exported HTTP status codes
4 changes: 2 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const program = Effect.gen(function* () {
console.log(users);
});

const matchOk = Match.type<Result.OkStatusCode>().pipe(
const matchOk = Match.type<Result.StatusOK>().pipe(
Match.when(200, () => "200"),
Match.when(201, () => "201"),
Match.when(202, () => "202"),
Expand All @@ -38,7 +38,7 @@ const matchNotOk = Match.type<Response>().pipe(

const status = Fetch.fetch("/users/2").pipe(
Effect.andThen(Result.filterStatusOk),
Effect.andThen((res) => matchOk(res.status as Result.OkStatusCode)),
Effect.andThen((res) => matchOk(res.status as Result.StatusOK)),
Effect.catchTag('StatusError', res => Effect.succeed(matchNotOk(res.response))),
Effect.andThen(Effect.log),
Effect.catchAll(Effect.log),
Expand Down
12 changes: 6 additions & 6 deletions src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
StatusError,
StatusErrorT,
} from "./internal/response/index.js";
import { NotOkStatusCode, OkStatusCode } from "./internal/response/types.js";
import { StatusNotOK, StatusOK } from "./internal/response/types.js";

export {
/**
Expand All @@ -22,16 +22,16 @@ export {
StatusCode,

/**
* @since 1.1.0
* @since 2.1.0
* @category status code
*/
OkStatusCode,
StatusOK,

/**
* @since 1.1.0
* @since 2.1.0
* @category status code
*/
NotOkStatusCode,
StatusNotOK,
} from "./internal/response/types.js";

export {
Expand Down Expand Up @@ -99,7 +99,7 @@ export const filterStatusOk: (
*/
export const filterStatusOkT: (
response: Response
) => Effect<ResponseT<OkStatusCode>, StatusErrorT<NotOkStatusCode>, never> =
) => Effect<ResponseT<StatusOK>, StatusErrorT<StatusNotOK>, never> =
internal.filterStatusOkT;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/internal/response/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Predicate } from "effect/Predicate";

import { DecodeError } from "../error.js";
import { decode } from "../utils.js";
import { NotOkStatusCode, OkStatusCode, StatusCode } from "./types.js";
import { StatusNotOK, StatusOK, StatusCode } from "./types.js";

export class StatusError {
readonly _tag = "StatusError";
Expand Down Expand Up @@ -41,8 +41,8 @@ export interface ResponseT<S extends StatusCode> extends Response {
export const filterStatusOkT = flow(
filterStatusOk,
Effect.mapBoth({
onSuccess: (r) => r as ResponseT<OkStatusCode>,
onFailure: (e) => e as StatusErrorT<NotOkStatusCode>,
onSuccess: (r) => r as ResponseT<StatusOK>,
onFailure: (e) => e as StatusErrorT<StatusNotOK>,
})
);

Expand Down
6 changes: 3 additions & 3 deletions src/internal/response/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type OkStatusCode =
export type StatusOK =
| 200 // OK
| 201 // Created
| 202 // Accepted
Expand All @@ -15,7 +15,7 @@ export type StatusCode =
| 101 // Switching Protocols
| 102 // Processing
| 103 // Early Hints
| OkStatusCode
| StatusOK
| 300 // Multiple Choices
| 301 // Moved Permanently
| 302 // Found
Expand Down Expand Up @@ -66,4 +66,4 @@ export type StatusCode =
| 510 // Not Extended
| 511; // Network Authentication Required

export type NotOkStatusCode = Exclude<StatusCode, OkStatusCode>
export type StatusNotOK = Exclude<StatusCode, StatusOK>

0 comments on commit a9b8769

Please sign in to comment.