Skip to content

Commit

Permalink
rename ApiClient to ApiClientFactory (#126)
Browse files Browse the repository at this point in the history
* rename ApiClient to ApiClientFactory

* further improve

* improve naming

* add changeset
  • Loading branch information
patroza authored Dec 17, 2024
1 parent 7d28f90 commit b1396c9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-sloths-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect-app": minor
---

refactor: rename ApiClient to ApiClientFactory
10 changes: 5 additions & 5 deletions packages/effect-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@
"default": "./_cjs/client.cjs"
}
},
"./client/apiClient": {
"./client/apiClientFactory": {
"import": {
"types": "./dist/client/apiClient.d.ts",
"default": "./dist/client/apiClient.js"
"types": "./dist/client/apiClientFactory.d.ts",
"default": "./dist/client/apiClientFactory.js"
},
"require": {
"types": "./dist/client/apiClient.d.ts",
"default": "./_cjs/client/apiClient.cjs"
"types": "./dist/client/apiClientFactory.d.ts",
"default": "./_cjs/client/apiClientFactory.cjs"
}
},
"./client/clientFor": {
Expand Down
2 changes: 1 addition & 1 deletion packages/effect-app/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// codegen:start {preset: barrel, include: ./client/*.ts}
export * from "./client/apiClient.js"
export * from "./client/apiClientFactory.js"
export * from "./client/clientFor.js"
export * from "./client/errors.js"
export * from "./client/req.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Req = S.Schema.All & {
config?: Record<string, any>
}

const apiClient = (config: ApiConfig) =>
const makeApiClientFactory = (config: ApiConfig) =>
Effect.gen(function*() {
const baseClient = yield* HttpClient.HttpClient
const client = baseClient.pipe(
Expand All @@ -44,22 +44,22 @@ const apiClient = (config: ApiConfig) =>
)
)

const clientFor_ = <M extends Requests>(models: M, requestLevelLayers = Layer.empty) => {
const makeClientFor = <M extends Requests>(resource: M, requestLevelLayers = Layer.empty) => {
type Filtered = {
[K in keyof Requests as Requests[K] extends Req ? K : never]: Requests[K] extends Req ? Requests[K] : never
}
// TODO: Record.filter
const filtered = typedKeysOf(models).reduce((acc, cur) => {
const filtered = typedKeysOf(resource).reduce((acc, cur) => {
if (
Predicate.isObject(models[cur])
&& (models[cur].success)
Predicate.isObject(resource[cur])
&& (resource[cur].success)
) {
acc[cur as keyof Filtered] = models[cur]
acc[cur as keyof Filtered] = resource[cur]
}
return acc
}, {} as Record<keyof Filtered, Req>)

const meta = (models as any).meta as { moduleName: string }
const meta = (resource as any).meta as { moduleName: string }
if (!meta) throw new Error("No meta defined in Resource!")

const resolver = flow(
Expand Down Expand Up @@ -145,7 +145,7 @@ const apiClient = (config: ApiConfig) =>
}, {} as Client<M>))
}

function makeFor(requestLevelLayers: Layer.Layer<never, never, never>) {
function makeClientForCached(requestLevelLayers: Layer.Layer<never, never, never>) {
const cache = new Map<any, Client<any>>()

return <M extends Requests>(
Expand All @@ -155,19 +155,27 @@ const apiClient = (config: ApiConfig) =>
if (found) {
return found
}
const m = clientFor_(models, requestLevelLayers)
const m = makeClientFor(models, requestLevelLayers)
cache.set(models, m)
return m
}
}

return { for_: clientFor_, makeFor_: makeFor }
return makeClientForCached
})

export class ApiClient extends Context.TagId("ApiClient")<ApiClient, Effect.Success<ReturnType<typeof apiClient>>>() {
static readonly layer = (apiConfig: ApiConfig) => this.toLayer(apiClient(apiConfig))
/**
* Used to create clients for resource modules.
*/
export class ApiClientFactory
extends Context.TagId("ApiClientFactory")<ApiClientFactory, Effect.Success<ReturnType<typeof makeApiClientFactory>>>()
{
static readonly layer = (config: ApiConfig) => this.toLayer(makeApiClientFactory(config))
static readonly layerFromConfig = DefaultApiConfig.pipe(Effect.map(this.layer), Layer.unwrapEffect)

static readonly makeFor = (requestLevelLayers: Layer.Layer<never, never, never>) => <M extends Requests>(models: M) =>
this.use((_) => _.makeFor_(requestLevelLayers)).pipe(Effect.map((_) => _(models)))
static readonly makeFor =
(requestLevelLayers: Layer.Layer<never, never, never>) => <M extends Requests>(resource: M) =>
this
.use((apiClientFactory) => apiClientFactory(requestLevelLayers))
.pipe(Effect.map((clientFor) => clientFor(resource)))
}

0 comments on commit b1396c9

Please sign in to comment.