Skip to content

Commit 36b9839

Browse files
committed
Add validation for undefined endpoint in TypeScript setEndpoint method
Previously, calling setEndpoint() with undefined would throw a confusing TypeError when trying to call .startsWith() on undefined. This adds proper validation to check if the endpoint is null, undefined, or not a string before checking its format, providing a clear error message. Changes: - Add endpoint validation before .startsWith() check - Throw descriptive error: "Endpoint must be a valid string" - Applied to Node, Web, Deno, and React Native TypeScript clients Fixes the weird error when endpoint is undefined.
1 parent ee434aa commit 36b9839

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

templates/deno/src/client.ts.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export class Client {
4646
* @return this
4747
*/
4848
setEndpoint(endpoint: string): this {
49+
if (!endpoint || typeof endpoint !== 'string') {
50+
throw new {{spec.title | caseUcfirst}}Exception('Endpoint must be a valid string');
51+
}
52+
4953
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
5054
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
5155
}

templates/node/src/client.ts.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ class Client {
9696
* @returns {this}
9797
*/
9898
setEndpoint(endpoint: string): this {
99+
if (!endpoint || typeof endpoint !== 'string') {
100+
throw new {{spec.title | caseUcfirst}}Exception('Endpoint must be a valid string');
101+
}
102+
99103
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
100104
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
101105
}

templates/react-native/src/client.ts.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ class Client {
129129
* @returns {this}
130130
*/
131131
setEndpoint(endpoint: string): this {
132+
if (!endpoint || typeof endpoint !== 'string') {
133+
throw new {{spec.title | caseUcfirst}}Exception('Endpoint must be a valid string');
134+
}
135+
132136
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
133137
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
134138
}

templates/web/src/client.ts.twig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ class Client {
334334
* @returns {this}
335335
*/
336336
setEndpoint(endpoint: string): this {
337+
if (!endpoint || typeof endpoint !== 'string') {
338+
throw new {{spec.title | caseUcfirst}}Exception('Endpoint must be a valid string');
339+
}
340+
337341
if (!endpoint.startsWith('http://') && !endpoint.startsWith('https://')) {
338342
throw new {{spec.title | caseUcfirst}}Exception('Invalid endpoint URL: ' + endpoint);
339343
}

0 commit comments

Comments
 (0)