Skip to content

Commit

Permalink
rename and export platform file schemas (#2610)
Browse files Browse the repository at this point in the history
Co-authored-by: Wayne Maurer <2899448+wmaurer@users.noreply.github.com>
  • Loading branch information
2 people authored and mikearnaldi committed Apr 25, 2024
1 parent ce0ec5c commit bb661ff
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .changeset/hungry-ghosts-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@effect/platform-node": minor
"@effect/platform-bun": minor
"@effect/platform": minor
---

* capitalised Http.multipart.FileSchema and Http.multipart.FilesSchema
* exported Http.multipart.FileSchema
* added Http.multipart.SingleFileSchema
2 changes: 1 addition & 1 deletion packages/platform-bun/examples/http-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const HttpLive = Http.router.empty.pipe(
"/upload",
Effect.gen(function*(_) {
const data = yield* _(Http.request.schemaBodyForm(Schema.Struct({
files: Http.multipart.filesSchema
files: Http.multipart.FilesSchema
})))
console.log("got files", data.files)
return Http.response.empty()
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-node/examples/http-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const HttpLive = Http.router.empty.pipe(
"/upload",
Effect.gen(function*(_) {
const data = yield* _(Http.request.schemaBodyForm(Schema.Struct({
files: Http.multipart.filesSchema
files: Http.multipart.FilesSchema
})))
console.log("got files", data.files)
return Http.response.empty()
Expand Down
2 changes: 1 addition & 1 deletion packages/platform-node/test/HttpServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe("HttpServer", () => {
"/upload",
Effect.gen(function*(_) {
const files = yield* _(Http.request.schemaBodyForm(Schema.Struct({
file: Http.multipart.filesSchema,
file: Http.multipart.FilesSchema,
test: Schema.String
})))
expect(files).toHaveProperty("file")
Expand Down
17 changes: 16 additions & 1 deletion packages/platform/src/Http/Multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,22 @@ export const withFieldMimeTypes: {
* @since 1.0.0
* @category schema
*/
export const filesSchema: Schema.Schema<ReadonlyArray<PersistedFile>> = internal.filesSchema
export const FileSchema: Schema.Schema<PersistedFile> = internal.FileSchema

/**
* @since 1.0.0
* @category schema
*/
export const FilesSchema: Schema.Schema<ReadonlyArray<PersistedFile>> = internal.FilesSchema

/**
* @since 1.0.0
* @category schema
*/
export const SingleFileSchema: Schema.transform<
Schema.Schema<ReadonlyArray<PersistedFile>>,
Schema.Schema<PersistedFile>
> = internal.SingleFileSchema

/**
* @since 1.0.0
Expand Down
14 changes: 12 additions & 2 deletions packages/platform/src/internal/http/multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,22 @@ export const withFieldMimeTypes = dual<
<R, E, A>(effect: Effect.Effect<A, E, R>, mimeTypes: ReadonlyArray<string>) => Effect.Effect<A, E, R>
>(2, (effect, mimeTypes) => Effect.locally(effect, fieldMimeTypes, Chunk.fromIterable(mimeTypes)))

const fileSchema: Schema.Schema<Multipart.PersistedFile> = Schema.declare(isPersistedFile, {
/** @internal */
export const FileSchema: Schema.Schema<Multipart.PersistedFile> = Schema.declare(isPersistedFile, {
identifier: "PersistedFile"
})

/** @internal */
export const filesSchema: Schema.Schema<ReadonlyArray<Multipart.PersistedFile>> = Schema.Array(fileSchema)
export const FilesSchema: Schema.Schema<ReadonlyArray<Multipart.PersistedFile>> = Schema.Array(FileSchema)

/** @internal */
export const SingleFileSchema: Schema.transform<
Schema.Schema<ReadonlyArray<Multipart.PersistedFile>>,
Schema.Schema<Multipart.PersistedFile>
> = Schema.transform(FilesSchema.pipe(Schema.itemsCount(1)), FileSchema, {
decode: ([file]) => file,
encode: (file) => [file]
})

/** @internal */
export const schemaPersisted = <R, I extends Partial<Multipart.Persisted>, A>(
Expand Down

0 comments on commit bb661ff

Please sign in to comment.