Skip to content

Commit 278640f

Browse files
committed
refactor: clean up createClient overloads, add examples for each way to use it and make compatible with typedoc
1 parent 832e72e commit 278640f

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

lib/contentful-management.ts

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,69 @@ interface UserAgentParams {
4545
export type ClientOptions = UserAgentParams & XOR<RestAdapterParams, AdapterParams>
4646

4747
/**
48-
* Create a client instance
49-
* @param clientOptions - Client initialization parameters
48+
* Create a plain client instance
5049
*
50+
* @param clientOptions
51+
* @param opts
52+
*
53+
* @example Plain Client
54+
* ```javascript
55+
* const client = contentfulManagement.createClient({
56+
* accessToken: 'myAccessToken',
57+
* opts: {
58+
* type: 'plain'
59+
* }
60+
* })
61+
* ```
62+
* @example Plain Client with defaults
5163
* ```javascript
5264
* const client = contentfulManagement.createClient({
53-
* accessToken: 'myAccessToken'
65+
* accessToken: 'myAccessToken',
66+
* opts: {
67+
* type: 'plain',
68+
* defaults: {
69+
* ...
70+
* }
71+
* }
5472
* })
5573
* ```
5674
*/
57-
function createClient(clientOptions: ClientOptions): ClientAPI
5875
function createClient(
5976
clientOptions: ClientOptions,
6077
opts: {
6178
type: 'plain'
6279
defaults?: DefaultParams
6380
}
6481
): PlainClientAPI
65-
// Usually, overloads with more specific signatures should come first but some IDEs are often not able to handle overloads with separate TSDocs correctly
82+
/**
83+
* Create a legacy, chainable client instance
84+
* @param clientOptions
85+
*
86+
* @example Legacy Chainable Client
87+
* ```javascript
88+
* const client = contentfulManagement.createClient({
89+
* accessToken: 'myAccessToken'
90+
* })
91+
* ```
92+
*/
93+
function createClient(clientOptions: ClientOptions): ClientAPI
94+
/**
95+
* Create a legacy or plain client instance
96+
*
97+
* Please check the responding section below:
98+
*
99+
* * [Plain Client](#createclient)
100+
* * [Legacy Chainable Client](#createclient-1)
101+
*/
66102
function createClient(
67103
clientOptions: ClientOptions,
68-
opts: {
69-
type?: 'plain'
104+
opts?: {
105+
type?: string
70106
defaults?: DefaultParams
71-
} = {}
107+
}
72108
): ClientAPI | PlainClientAPI {
73109
const sdkMain =
74-
opts.type === 'plain' ? 'contentful-management-plain.js' : 'contentful-management.js'
110+
opts && opts.type === 'plain' ? 'contentful-management-plain.js' : 'contentful-management.js'
75111
const userAgent = getUserAgentHeader(
76112
// @ts-expect-error
77113
`${sdkMain}/${__VERSION__}`,
@@ -88,7 +124,7 @@ function createClient(
88124
const makeRequest: MakeRequest = (options: Parameters<MakeRequest>[0]): ReturnType<MakeRequest> =>
89125
adapter.makeRequest({ ...options, userAgent })
90126

91-
if (opts.type === 'plain') {
127+
if (opts && opts.type === 'plain') {
92128
return createPlainClient(makeRequest, opts.defaults)
93129
} else {
94130
return createContentfulApi(makeRequest) as ClientAPI

0 commit comments

Comments
 (0)