-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50e8bf8
commit 17516af
Showing
19 changed files
with
1,035 additions
and
529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { forIn } from "lodash-es"; | ||
|
||
import { request } from "./request"; | ||
import { Api3 } from "./types"; | ||
import { RequestBuilder } from "./request-builder"; | ||
import { ClientConfig } from "../types"; | ||
|
||
export function createApi3Client<E extends Api3.EntitiesConfig>( | ||
config: ClientConfig<any, E>, | ||
) { | ||
const client = {} as Api3.Client<E>; | ||
|
||
forIn(config.api3!.entities, ({ name, actions }: any, entity: string) => { | ||
Reflect.defineProperty(client, entity, { | ||
get: () => | ||
new RequestBuilder( | ||
name, | ||
(requestParams, requestOptions) => | ||
request.bind(config)(requestParams, { | ||
...config.requestOptions, | ||
...requestOptions, | ||
}), | ||
actions, | ||
), | ||
}); | ||
}); | ||
|
||
return client; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { forIn } from "lodash-es"; | ||
|
||
import { Api3 } from "./types"; | ||
import { RequestBuilder as BaseRequestBuilder } from "../lib/request-builder"; | ||
|
||
export class RequestBuilder< | ||
A extends Api3.Actions, | ||
Response = any, | ||
> extends BaseRequestBuilder<Api3.RequestParams, Response> { | ||
private action: string; | ||
private params?: Api3.Params; | ||
private opts: Record<string, Api3.Value> = {}; | ||
|
||
constructor(entity: string, request: Api3.RequestFn<Response>, actions: A) { | ||
super(entity, request); | ||
|
||
forIn(actions, (action: string, key: string) => { | ||
this[key] = (params?: Api3.Params) => { | ||
this.action = action as any; | ||
this.params = params; | ||
|
||
return this; | ||
}; | ||
}); | ||
} | ||
|
||
get requestParams(): Api3.RequestParams { | ||
return [this.entity, this.action, this.params, this.opts]; | ||
} | ||
|
||
addOption(option: string, value: Api3.Value) { | ||
this.opts[option] = value; | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { isEmpty } from "lodash-es"; | ||
|
||
import { Api3 } from "./types"; | ||
import { request as baseRequest } from "../lib/request"; | ||
|
||
const path = "civicrm/ajax/rest"; | ||
|
||
export async function request( | ||
this: { | ||
baseUrl: string; | ||
apiKey: string; | ||
debug?: boolean; | ||
}, | ||
[entity, action, params, options]: Api3.RequestParams, | ||
requestOptions: RequestInit = {}, | ||
) { | ||
const json = isEmpty(options) ? { ...params } : { ...params, options }; | ||
|
||
const searchParams = new URLSearchParams({ | ||
entity: entity, | ||
action: action, | ||
json: JSON.stringify(json), | ||
}); | ||
|
||
return baseRequest.bind(this)(path, searchParams, requestOptions); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { RequestBuilder } from "./request-builder"; | ||
import { BaseRequestFn } from "../types"; | ||
|
||
export namespace Api3 { | ||
export type EntitiesConfig = { | ||
[key: string]: { | ||
name: string; | ||
actions: { | ||
[key: string]: string; | ||
}; | ||
}; | ||
}; | ||
|
||
export type Actions = { | ||
[key: string]: string; | ||
}; | ||
export type ActionMethods<A extends Actions> = Record< | ||
keyof A, | ||
(params?: Api3.Params) => RequestBuilder<A> | ||
>; | ||
|
||
export type Client<E extends EntitiesConfig> = { | ||
[K in keyof E]: RequestBuilder<E[K]["actions"]> & | ||
ActionMethods<E[K]["actions"]>; | ||
}; | ||
|
||
export type Value = | ||
| string | ||
| number | ||
| string[] | ||
| number[] | ||
| boolean | ||
| boolean[] | ||
| null; | ||
|
||
export type Params = Record<string, Value>; | ||
export type Options = Record<string, Value>; | ||
|
||
export type RequestParams = [string, string, Params?, Options?]; | ||
export type RequestFn<Response> = BaseRequestFn<RequestParams, Response>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { forIn } from "lodash-es"; | ||
|
||
import { request } from "./request"; | ||
import { Api4 } from "./types"; | ||
import { RequestBuilder } from "./request-builder"; | ||
import { ClientConfig } from "../types"; | ||
|
||
export function createApi4Client<E extends Api4.EntitiesConfig>( | ||
config: ClientConfig<E, any>, | ||
) { | ||
const client = {} as Api4.Client<E>; | ||
|
||
forIn(config.entities, (entity: string, key: string) => { | ||
Reflect.defineProperty(client, key, { | ||
get: () => | ||
new RequestBuilder(entity, (requestParams, requestOptions) => | ||
request.bind(config)(requestParams, { | ||
...config.requestOptions, | ||
...requestOptions, | ||
}), | ||
), | ||
}); | ||
}); | ||
|
||
return client; | ||
} |
Oops, something went wrong.