Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
rename schemaBodyMultipartJson to schemaBodyFormJson & support url fo…
Browse files Browse the repository at this point in the history
…rms (#371)
  • Loading branch information
tim-smart authored Dec 20, 2023
1 parent 5d5f62b commit 49fb154
Show file tree
Hide file tree
Showing 13 changed files with 339 additions and 71 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-penguins-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": minor
---

rename schemaBodyMultipartJson to schemaBodyFormJson & support url forms
11 changes: 11 additions & 0 deletions docs/platform/Http/Body.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Added in v1.0.0
- [text](#text)
- [uint8Array](#uint8array)
- [unsafeJson](#unsafejson)
- [urlParams](#urlparams)
- [errors](#errors)
- [BodyError](#bodyerror)
- [BodyError (interface)](#bodyerror-interface)
Expand Down Expand Up @@ -184,6 +185,16 @@ export declare const unsafeJson: (body: unknown) => Uint8Array
Added in v1.0.0
## urlParams
**Signature**
```ts
export declare const urlParams: (urlParams: UrlParams.UrlParams) => Uint8Array
```
Added in v1.0.0
# errors
## BodyError
Expand Down
4 changes: 2 additions & 2 deletions docs/platform/Http/Multipart.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ Added in v1.0.0
export declare const schemaJson: <I, A>(
schema: Schema.Schema<I, A>
) => {
(field: string): (persisted: Persisted) => Effect.Effect<never, MultipartError | ParseResult.ParseError, A>
(persisted: Persisted, field: string): Effect.Effect<never, MultipartError | ParseResult.ParseError, A>
(field: string): (persisted: Persisted) => Effect.Effect<never, ParseResult.ParseError, A>
(persisted: Persisted, field: string): Effect.Effect<never, ParseResult.ParseError, A>
}
```
Expand Down
34 changes: 17 additions & 17 deletions docs/platform/Http/ServerRequest.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Added in v1.0.0
- [ServerRequest (interface)](#serverrequest-interface)
- [schema](#schema)
- [schemaBodyForm](#schemabodyform)
- [schemaBodyFormJson](#schemabodyformjson)
- [schemaBodyJson](#schemabodyjson)
- [schemaBodyMultipart](#schemabodymultipart)
- [schemaBodyMultipartJson](#schemabodymultipartjson)
- [schemaBodyUrlParams](#schemabodyurlparams)
- [schemaHeaders](#schemaheaders)
- [type ids](#type-ids)
Expand Down Expand Up @@ -129,53 +129,53 @@ export declare const schemaBodyForm: <I extends Multipart.Persisted, A>(
schema: Schema.Schema<I, A>
) => Effect.Effect<
Scope.Scope | Path.Path | FileSystem.FileSystem | ServerRequest,
Multipart.MultipartError | ParseResult.ParseError | Error.RequestError,
ParseResult.ParseError | Multipart.MultipartError | Error.RequestError,
A
>
```
Added in v1.0.0
## schemaBodyJson
## schemaBodyFormJson
**Signature**
```ts
export declare const schemaBodyJson: <I, A>(
export declare const schemaBodyFormJson: <I, A>(
schema: Schema.Schema<I, A>
) => Effect.Effect<ServerRequest, ParseResult.ParseError | Error.RequestError, A>
) => (
field: string
) => Effect.Effect<
Scope.Scope | Path.Path | FileSystem.FileSystem | ServerRequest,
ParseResult.ParseError | Error.RequestError,
A
>
```
Added in v1.0.0
## schemaBodyMultipart
## schemaBodyJson
**Signature**
```ts
export declare const schemaBodyMultipart: <I extends Multipart.Persisted, A>(
export declare const schemaBodyJson: <I, A>(
schema: Schema.Schema<I, A>
) => Effect.Effect<
Scope.Scope | Path.Path | FileSystem.FileSystem | ServerRequest,
Multipart.MultipartError | ParseResult.ParseError,
A
>
) => Effect.Effect<ServerRequest, ParseResult.ParseError | Error.RequestError, A>
```
Added in v1.0.0
## schemaBodyMultipartJson
## schemaBodyMultipart
**Signature**
```ts
export declare const schemaBodyMultipartJson: <I, A>(
export declare const schemaBodyMultipart: <I extends Multipart.Persisted, A>(
schema: Schema.Schema<I, A>
) => (
field: string
) => Effect.Effect<
Scope.Scope | Path.Path | FileSystem.FileSystem | ServerRequest,
Multipart.MultipartError | ParseResult.ParseError | Error.RequestError,
ParseResult.ParseError | Multipart.MultipartError,
A
>
```
Expand Down
61 changes: 61 additions & 0 deletions docs/platform/Http/UrlParams.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ Added in v1.0.0
- [combinators](#combinators)
- [append](#append)
- [appendAll](#appendall)
- [getAll](#getall)
- [getFirst](#getfirst)
- [getLast](#getlast)
- [remove](#remove)
- [set](#set)
- [setAll](#setall)
Expand All @@ -26,6 +29,8 @@ Added in v1.0.0
- [models](#models)
- [Input (type alias)](#input-type-alias)
- [UrlParams (interface)](#urlparams-interface)
- [schema](#schema)
- [schemaJson](#schemajson)

---

Expand Down Expand Up @@ -57,6 +62,45 @@ export declare const appendAll: {
Added in v1.0.0
## getAll
**Signature**
```ts
export declare const getAll: {
(key: string): (self: UrlParams) => ReadonlyArray<string>
(self: UrlParams, key: string): ReadonlyArray<string>
}
```
Added in v1.0.0
## getFirst
**Signature**
```ts
export declare const getFirst: {
(key: string): (self: UrlParams) => Option.Option<string>
(self: UrlParams, key: string): Option.Option<string>
}
```
Added in v1.0.0
## getLast
**Signature**
```ts
export declare const getLast: {
(key: string): (self: UrlParams) => Option.Option<string>
(self: UrlParams, key: string): Option.Option<string>
}
```
Added in v1.0.0
## remove
**Signature**
Expand Down Expand Up @@ -163,3 +207,20 @@ export interface UrlParams extends ReadonlyArray<readonly [string, string]> {}
```

Added in v1.0.0

# schema

## schemaJson

**Signature**

```ts
export declare const schemaJson: <I, A>(
schema: Schema.Schema<I, A>
) => {
(field: string): (self: UrlParams) => Effect.Effect<never, ParseResult.ParseError, A>
(self: UrlParams, field: string): Effect.Effect<never, ParseResult.ParseError, A>
}
```
Added in v1.0.0
92 changes: 92 additions & 0 deletions packages/platform-node/test/HttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,98 @@ describe("HttpServer", () => {
expect(response.status).toEqual(400)
}).pipe(Effect.scoped, runPromise))

it("schemaBodyFormJson", () =>
Effect.gen(function*(_) {
yield* _(
Http.router.empty,
Http.router.post(
"/upload",
Effect.gen(function*(_) {
const result = yield* _(
Http.request.schemaBodyFormJson(Schema.struct({
test: Schema.string
}))("json")
)
expect(result.test).toEqual("content")
return Http.response.empty()
}).pipe(Effect.scoped)
),
Effect.tapErrorCause(Effect.logError),
Http.server.serveEffect()
)
const client = yield* _(makeClient)
const formData = new FormData()
formData.append("json", JSON.stringify({ test: "content" }))
const response = yield* _(
client(HttpC.request.post("/upload", { body: HttpC.body.formData(formData) }))
)
expect(response.status).toEqual(204)
}).pipe(Effect.scoped, runPromise))

it("schemaBodyFormJson file", () =>
Effect.gen(function*(_) {
yield* _(
Http.router.empty,
Http.router.post(
"/upload",
Effect.gen(function*(_) {
const result = yield* _(
Http.request.schemaBodyFormJson(Schema.struct({
test: Schema.string
}))("json")
)
expect(result.test).toEqual("content")
return Http.response.empty()
}).pipe(Effect.scoped)
),
Effect.tapErrorCause(Effect.logError),
Http.server.serveEffect()
)
const client = yield* _(makeClient)
const formData = new FormData()
formData.append(
"json",
new Blob([JSON.stringify({ test: "content" })], { type: "application/json" }),
"test.json"
)
const response = yield* _(
client(HttpC.request.post("/upload", { body: HttpC.body.formData(formData) }))
)
expect(response.status).toEqual(204)
}).pipe(Effect.scoped, runPromise))

it("schemaBodyFormJson url encoded", () =>
Effect.gen(function*(_) {
yield* _(
Http.router.empty,
Http.router.post(
"/upload",
Effect.gen(function*(_) {
const result = yield* _(
Http.request.schemaBodyFormJson(Schema.struct({
test: Schema.string
}))("json")
)
expect(result.test).toEqual("content")
return Http.response.empty()
}).pipe(Effect.scoped)
),
Effect.tapErrorCause(Effect.logError),
Http.server.serveEffect()
)
const client = yield* _(makeClient)
const response = yield* _(
client(
HttpC.request.post("/upload", {
body: HttpC.body.urlParams(HttpC.urlParams.fromInput({
json: JSON.stringify({ test: "content" })
}))
})
)
)
expect(response.status).toEqual(204)
}).pipe(Effect.scoped, runPromise))

it("tracing", () =>
Effect.gen(function*(_) {
yield* _(
Expand Down
7 changes: 7 additions & 0 deletions packages/platform/src/Http/Body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type * as Effect from "effect/Effect"
import type * as Stream_ from "effect/Stream"
import type * as PlatformError from "../Error.js"
import type * as FileSystem from "../FileSystem.js"
import type * as UrlParams from "../Http/UrlParams.js"
import * as internal from "../internal/http/body.js"

/**
Expand Down Expand Up @@ -167,6 +168,12 @@ export const jsonSchema: <I, A>(
schema: Schema.Schema<I, A>
) => (body: A) => Effect.Effect<never, BodyError, Uint8Array> = internal.jsonSchema

/**
* @since 1.0.0
* @category constructors
*/
export const urlParams: (urlParams: UrlParams.UrlParams) => Uint8Array = internal.urlParams

/**
* @since 1.0.0
* @category models
Expand Down
4 changes: 2 additions & 2 deletions packages/platform/src/Http/Multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ export const filesSchema: Schema.Schema<ReadonlyArray<PersistedFile>, ReadonlyAr
export const schemaJson: <I, A>(
schema: Schema.Schema<I, A>
) => {
(field: string): (persisted: Persisted) => Effect.Effect<never, MultipartError | ParseResult.ParseError, A>
(persisted: Persisted, field: string): Effect.Effect<never, MultipartError | ParseResult.ParseError, A>
(field: string): (persisted: Persisted) => Effect.Effect<never, ParseResult.ParseError, A>
(persisted: Persisted, field: string): Effect.Effect<never, ParseResult.ParseError, A>
} = internal.schemaJson

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/platform/src/Http/ServerRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ export const schemaBodyMultipart: <I extends Multipart.Persisted, A>(
* @since 1.0.0
* @category schema
*/
export const schemaBodyMultipartJson: <I, A>(
export const schemaBodyFormJson: <I, A>(
schema: Schema.Schema<I, A>
) => (
field: string
) => Effect.Effect<
ServerRequest | Scope.Scope | FileSystem.FileSystem | Path.Path,
Error.RequestError | Multipart.MultipartError | ParseResult.ParseError,
Error.RequestError | ParseResult.ParseError,
A
> = internal.schemaBodyMultipartJson
> = internal.schemaBodyFormJson

/**
* @since 1.0.0
Expand Down
Loading

0 comments on commit 49fb154

Please sign in to comment.