import { HttpClient, HttpClientRequest, HttpClientResponse, UrlParams } from '@effect/platform'
import { Effect, Schema } from 'effect'
export function createUrlEncodedPost<
RequestData, // The plain TS object for the request
RequestEncoded extends Record<string, any>, // The encoded request object
ResponseRaw, // The raw JSON response
ResponseParsed, // The final parsed response object
>(
requestSchema: Schema.Schema<RequestData, RequestEncoded>,
responseSchema: Schema.Schema<ResponseRaw, ResponseParsed>,
url: string,
) {
const encodeRequest = Schema.encode(requestSchema)
/**
* @param params The data for the request body, matching the `requestSchema`.
* @returns An Effect describing the full HTTP request and response cycle.
*/
return (params: RequestData) => {
return Effect.gen(function* () {
// 1. Encode the input parameters, which also validates them against the requestSchema.
const encodedParams = yield* encodeRequest(params)
const body = UrlParams.fromInput(Object.entries(encodedParams))
// 2. Build the POST request with the encoded body.
const request = HttpClientRequest.post(url).pipe(HttpClientRequest.bodyUrlParams(body))
// 3. Execute the request using the provided HttpClient service.
const response = yield* HttpClient.execute(request)
// 4. Decode the JSON response against the responseSchema.
return yield* HttpClientResponse.schemaBodyJson(responseSchema)(response)
})
}
}
import { HttpClient, HttpClientRequest, HttpClientResponse, UrlParams } from '@effect/platform'
import { Effect, Schema } from 'effect'
export function createUrlEncodedPost<
RequestData, // The plain TS object for the request
RequestEncoded extends Record<string, any>, // The encoded request object
ResponseRaw, // The raw JSON response
ResponseParsed, // The final parsed response object
>(
requestSchema: Schema.Schema<RequestData, RequestEncoded>,
responseSchema: Schema.Schema<ResponseRaw, ResponseParsed>,
url: string,
) {
const encodeRequest = Schema.encode(requestSchema)
/**
* @param params The data for the request body, matching the `requestSchema`.
* @returns An Effect describing the full HTTP request and response cycle.
*/
return (params: RequestData) => {
return Effect.gen(function* () {
// 1. Encode the input parameters, which also validates them against the requestSchema.
const encodedParams = yield* encodeRequest(params)
const body = UrlParams.fromInput(Object.entries(encodedParams))
// 2. Build the POST request with the encoded body.
const request = HttpClientRequest.post(url).pipe(HttpClientRequest.bodyUrlParams(body))
// 3. Execute the request using the provided HttpClient service.
const response = yield* HttpClient.execute(request)
// 4. Decode the JSON response against the responseSchema.
return yield* HttpClientResponse.schemaBodyJson(responseSchema)(response)
})
}
}