Skip to content

Commit 525637a

Browse files
committed
Add overload function to prevent breaking change for assets .list() API
1 parent d1b7578 commit 525637a

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/wrapper/AssetsClient.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Assets } from "../api/resources/assets/client/Client";
22
import { Client as Utilities } from "./AssetsUtilitiesClient";
3+
import * as Webflow from "../api/index";
4+
import * as core from "../core";
35

46
// Extends the namespace declared in the Fern generated client
57
declare module "../api/resources/assets/client/Client" {
@@ -19,6 +21,90 @@ export class Client extends Assets {
1921
constructor(protected readonly _options: Assets.Options) {
2022
super(_options);
2123
}
24+
// IMPORTANT: Need to keep function overload signatures for the list() API for backwards compatibility.
25+
// In v.3.2.1, we are introducing a new 2nd parameter for the request object which allows
26+
// for pagination options to be passed in
27+
28+
/**
29+
* List of assets uploaded to a site
30+
*
31+
* Supports both the old 2-parameter API and new 3-parameter API for backward compatibility
32+
*
33+
* Required scope | `assets:read`
34+
*
35+
* @param {string} siteId - Unique identifier for a Site
36+
* @param {Assets.RequestOptions} requestOptions - Request-specific configuration.
37+
*/
38+
public list(siteId: string, requestOptions?: Assets.RequestOptions): core.HttpResponsePromise<Webflow.Assets>;
39+
40+
/**
41+
* List of assets uploaded to a site
42+
*
43+
* Required scope | `assets:read`
44+
*
45+
* @param {string} siteId - Unique identifier for a Site
46+
* @param {Webflow.AssetsListRequest} request
47+
* @param {Assets.RequestOptions} requestOptions - Request-specific configuration.
48+
*/
49+
public list(
50+
siteId: string,
51+
request: Webflow.AssetsListRequest,
52+
requestOptions?: Assets.RequestOptions,
53+
): core.HttpResponsePromise<Webflow.Assets>;
54+
55+
public list(
56+
siteId: string,
57+
requestOrOptions?: Webflow.AssetsListRequest | Assets.RequestOptions,
58+
requestOptions?: Assets.RequestOptions,
59+
): core.HttpResponsePromise<Webflow.Assets> {
60+
// Check if the second parameter is RequestOptions (old API) or AssetsListRequest (new API)
61+
// RequestOptions has properties like timeoutInSeconds, maxRetries, abortSignal, headers
62+
// AssetsListRequest has properties like offset, limit
63+
const isOldApi = this._isRequestOptions(requestOrOptions);
64+
65+
if (isOldApi) {
66+
// Old 2-parameter API: list(siteId, requestOptions)
67+
return super.list(siteId, {}, requestOrOptions as Assets.RequestOptions);
68+
} else {
69+
// New 3-parameter API: list(siteId, request, requestOptions)
70+
return super.list(siteId, requestOrOptions as Webflow.AssetsListRequest, requestOptions);
71+
}
72+
}
73+
74+
private _isRequestOptions(param?: Webflow.AssetsListRequest | Assets.RequestOptions): param is Assets.RequestOptions {
75+
if (!param) {
76+
return false;
77+
}
78+
// Check if it has RequestOptions-specific properties
79+
const hasRequestOptionsProps =
80+
"timeoutInSeconds" in param ||
81+
"maxRetries" in param ||
82+
"abortSignal" in param;
83+
84+
// Check if it has AssetsListRequest-specific properties
85+
const hasAssetsListRequestProps = "offset" in param || "limit" in param;
86+
87+
// If it has RequestOptions properties, it's RequestOptions
88+
if (hasRequestOptionsProps) {
89+
return true;
90+
}
91+
92+
// If it has AssetsListRequest properties, it's not RequestOptions
93+
if (hasAssetsListRequestProps) {
94+
return false;
95+
}
96+
97+
// If it only has headers, check if there are other properties
98+
// headers can be in both types, so we need to be careful
99+
if ("headers" in param) {
100+
// If only headers property exists, assume it's RequestOptions
101+
const keys = Object.keys(param);
102+
return keys.length === 1 && keys[0] === "headers";
103+
}
104+
105+
// Default to treating empty object as AssetsListRequest (new API)
106+
return false;
107+
}
22108

23109
protected _utilities: Utilities | undefined;
24110

0 commit comments

Comments
 (0)