New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: remove ts-ignore by converting to string[] #14622
Open
kaleem68
wants to merge
11
commits into
calcom:main
Choose a base branch
from
kaleem68:fix/14621
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8473654
fix: remove ts-ignore by converting to string[]
kaleem68 cf62f1d
chore: remove extra space
kaleem68 2ed5ea3
chore: make word lower case
kaleem68 91cd67b
Merge branch 'main' into fix/14621
kaleem68 2a60a09
Merge branch 'main' into fix/14621
kaleem68 3e8c7cb
Merge branch 'main' into fix/14621
kaleem68 66f06ab
Merge branch 'main' into fix/14621
kaleem68 bc0c015
Merge branch 'main' into fix/14621
kaleem68 d138800
Merge branch 'main' into fix/14621
kaleem68 bf74379
Merge branch 'main' into fix/14621
kaleem68 9544cb3
Possible fix for endpoints.ts
emrysal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,19 +9,19 @@ type BaseEndpointDeclaration = { | |
// Define a type for static URIs | ||
type StaticUriEndpoint = BaseEndpointDeclaration & { | ||
uri: string; | ||
constructUri?: never; // Ensure constructUri is not used here | ||
constructUriFromObject?: never; | ||
constructUriFromArray?: never; | ||
}; | ||
|
||
// Introduce a generic type for dynamic URIs, allowing for explicit parameter type declaration | ||
type DynamicUriEndpoint<ParamsType extends Record<string, string> | string[]> = BaseEndpointDeclaration & { | ||
type DynamicUriEndpoint = BaseEndpointDeclaration & { | ||
uri?: never; // Ensure uri is not used here | ||
constructUri: (params: ParamsType) => string; | ||
constructUriFromArray?: (params: string[]) => string; | ||
constructUriFromObject?: (params: Record<string, string>) => string; | ||
}; | ||
|
||
// Create a discriminated union of the two types, incorporating generics | ||
type EndpointDeclaration<ParamsType extends Record<string, string> | string[] = string[]> = | ||
| StaticUriEndpoint | ||
| DynamicUriEndpoint<ParamsType>; | ||
type EndpointDeclaration = StaticUriEndpoint | DynamicUriEndpoint; | ||
|
||
export enum Endpoints { | ||
RESERVE_SLOT = "RESERVE_SLOT", | ||
|
@@ -50,29 +50,37 @@ const publicEndpoint = (uri: string, version = ApiVersion.NEUTRAL): EndpointDecl | |
apiVersion: version, | ||
}); | ||
|
||
// Adjust the constructor to use generics for flexible parameter typing | ||
const publicEndpointConstructor = <ParamsType extends Record<string, string> | string[]>( | ||
constructUri: (params: ParamsType) => string, | ||
version = ApiVersion.NEUTRAL | ||
): EndpointDeclaration<ParamsType> => ({ | ||
constructUri, | ||
auth: "public", | ||
apiVersion: version, | ||
}); | ||
const constructUri = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggestion
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update params for Suggestion
|
||
endpointData: { | ||
constructUriFromArray?: (params: string[]) => string; | ||
constructUriFromObject?: (params: Record<string, string>) => string; | ||
}, | ||
params: string[] | Record<string, string> | ||
) => { | ||
if (endpointData.constructUriFromObject && isParamsRecord(params)) { | ||
return endpointData.constructUriFromObject(params); | ||
} | ||
if (endpointData.constructUriFromArray && Array.isArray(params)) { | ||
return endpointData.constructUriFromArray(params); | ||
} | ||
throw new Error("Invalid parameter type for dynamic endpoint."); | ||
}; | ||
|
||
const ENDPOINTS: Record<Endpoints, EndpointDeclaration> = { | ||
RESERVE_SLOT: publicEndpoint("slots/reserve", ApiVersion.V2), | ||
AVAILABLE_SLOTS: publicEndpoint("slots/available", ApiVersion.V2), | ||
DELETE_SELECTED_SLOT: publicEndpoint("slots/delete", ApiVersion.V2), | ||
GET_PUBLIC_EVENT: publicEndpoint("events/"), | ||
EXCHANGE_OAUTH_AUTH_TOKEN: publicEndpointConstructor<string[]>( | ||
([clientId]) => `oauth/${clientId}/exchange`, | ||
ApiVersion.V2 | ||
), | ||
REFRESH_OAUTH_TOKEN: publicEndpointConstructor<string[]>( | ||
([clientId]) => `oauth/${clientId}/refresh`, | ||
ApiVersion.V2 | ||
), | ||
EXCHANGE_OAUTH_AUTH_TOKEN: { | ||
auth: "public", | ||
constructUriFromArray: ([clientId]) => `oauth/${clientId}/exchange`, | ||
apiVersion: ApiVersion.V2, | ||
}, | ||
REFRESH_OAUTH_TOKEN: { | ||
auth: "public", | ||
constructUriFromArray: ([clientId]) => `oauth/${clientId}/refresh`, | ||
apiVersion: ApiVersion.V2, | ||
}, | ||
CREATE_EVENT_TYPE: { | ||
apiVersion: ApiVersion.V2, | ||
auth: "access_token", | ||
|
@@ -81,9 +89,7 @@ const ENDPOINTS: Record<Endpoints, EndpointDeclaration> = { | |
GET_EVENT_TYPE_BY_ID: { | ||
apiVersion: ApiVersion.V2, | ||
auth: "access_token", | ||
constructUri([eventTypeId]) { | ||
return `event-types/${eventTypeId}`; | ||
}, | ||
constructUriFromArray: ([eventTypeId]) => `event-types/${eventTypeId}`, | ||
}, | ||
CREATE_SCHEDULE: { | ||
apiVersion: ApiVersion.V2, | ||
|
@@ -103,45 +109,33 @@ const ENDPOINTS: Record<Endpoints, EndpointDeclaration> = { | |
GET_SCHEDULE_BY_ID: { | ||
apiVersion: ApiVersion.V2, | ||
auth: "access_token", | ||
constructUri([scheduleId]) { | ||
return `schedules/${scheduleId}`; | ||
}, | ||
constructUriFromArray: ([scheduleId]) => `schedules/${scheduleId}`, | ||
}, | ||
GET_SUPPORTED_TIMEZONES: publicEndpoint("schedules/time-zones", ApiVersion.V2), | ||
UPDATE_SCHEDULE_BY_ID: { | ||
apiVersion: ApiVersion.V2, | ||
auth: "access_token", | ||
constructUri([scheduleId]) { | ||
return `schedules/${scheduleId}`; | ||
}, | ||
constructUriFromArray: ([scheduleId]) => `schedules/${scheduleId}`, | ||
}, | ||
DELETE_SCHEDULE_BY_ID: { | ||
apiVersion: ApiVersion.V2, | ||
auth: "access_token", | ||
constructUri([scheduleId]) { | ||
return `schedules/${scheduleId}`; | ||
}, | ||
constructUriFromArray: ([scheduleId]) => `schedules/${scheduleId}`, | ||
}, | ||
GET_MANAGED_USERS_BY_CLIENT_ID: { | ||
apiVersion: ApiVersion.V2, | ||
auth: "secret", | ||
constructUri([clientId]) { | ||
return `oauth-clients/${clientId}/users`; | ||
}, | ||
constructUriFromArray: ([clientId]) => `oauth-clients/${clientId}/users`, | ||
}, | ||
CREATE_MANAGED_USER: { | ||
apiVersion: ApiVersion.V2, | ||
auth: "secret", | ||
constructUri([clientId]) { | ||
return `oauth-clients/${clientId}/users`; | ||
}, | ||
constructUriFromArray: ([clientId]) => `oauth-clients/${clientId}/users`, | ||
}, | ||
GET_MANAGED_USER_BY_ID: { | ||
apiVersion: ApiVersion.V2, | ||
auth: "secret", | ||
constructUri([clientId, userId]) { | ||
return `oauth-clients/${clientId}/users/${userId}`; | ||
}, | ||
constructUriFromArray: ([clientId, userId]) => `oauth-clients/${clientId}/users/${userId}`, | ||
}, | ||
} as const; | ||
|
||
|
@@ -163,38 +157,28 @@ export const getEndpointData = ( | |
} => { | ||
const endpointData = ENDPOINTS[endpoint]; | ||
|
||
// Determine if the endpoint expects a dynamic URI construction | ||
if (typeof endpointData.constructUri === "function") { | ||
if (!params) { | ||
throw new Error(`Parameters are required for dynamic ${endpoint} endpoint.`); | ||
} | ||
|
||
// Here, we need to determine the correct type of params at runtime | ||
let constructedUri: string; | ||
if (isParamsRecord(params)) { | ||
// Params is a Record<string, string>, handle accordingly | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
constructedUri = endpointData.constructUri(params as Record<string, string>); | ||
} else if (Array.isArray(params)) { | ||
// Params is string[], handle accordingly | ||
constructedUri = endpointData.constructUri(params as string[]); | ||
} else { | ||
throw new Error("Invalid parameter type for dynamic endpoint."); | ||
} | ||
|
||
return { | ||
version: endpointData.apiVersion, | ||
uri: constructedUri, | ||
auth: endpointData.auth, | ||
}; | ||
} else if (endpointData.uri) { | ||
if (endpointData.uri) { | ||
return { | ||
version: endpointData.apiVersion, | ||
uri: endpointData.uri, | ||
auth: endpointData.auth, | ||
}; | ||
} else { | ||
} | ||
|
||
if (!params) { | ||
throw new Error(`Parameters are required for dynamic ${endpoint} endpoint.`); | ||
} | ||
|
||
if ( | ||
typeof endpointData.constructUriFromArray !== "function" && | ||
typeof endpointData.constructUriFromObject !== "function" | ||
) { | ||
throw new Error(`Endpoint configuration error for ${endpoint}`); | ||
} | ||
|
||
return { | ||
version: endpointData.apiVersion, | ||
uri: constructUri(endpointData, params), | ||
auth: endpointData.auth, | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion
// Define a type to avoid verbosity
type StrArrOrRecordStrStr = string[] | Record<string, string>;