Skip to content

Commit

Permalink
fix: added failsafe mechanism which tries to correct wrong usage of u…
Browse files Browse the repository at this point in the history
…rl building
  • Loading branch information
liquiddevelopmentnet committed Dec 26, 2023
1 parent 3975b16 commit f728ab6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/service/service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios'
import { ParamType, ServiceBuilder, Transient } from '..'
import { version } from '../../package.json'
import { addLeadingSlash, removeTrailingSlashes } from '../utils'

export interface PrivateProps {
endpoints: {
Expand Down Expand Up @@ -136,9 +137,10 @@ export class Service {
@Transient
private _buildEndpointFunction(name: string) {
const endpoint = this._p_props.endpoints[name]
const pre_url = `${this._g_props?.url}${this._p_props.suffix ?? ''}${
endpoint.url
}`
const pre_url =
removeTrailingSlashes(this._g_props?.url ?? '') +
(addLeadingSlash(this._p_props.suffix) ?? '') +
(addLeadingSlash(endpoint.url) ?? '')

this[name as keyof Service] = async (...args: object[]) => {
const url = endpoint.params
Expand Down
17 changes: 15 additions & 2 deletions src/service/serviceBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AxiosRequestConfig } from 'axios'
import { Service } from '..'
import { removeTrailingSlashes } from '../utils'

/**
* Options for configuring the ServiceBuilder.
Expand All @@ -26,15 +27,27 @@ export class ServiceBuilder {
this.options = options
}

private resolvePotentialSchemaInHost() {
if (this.options.host.startsWith('http://')) {
this.options.secure = false
this.options.host = this.options.host.replace(/^http:\/\//, '')
}
if (this.options.host.startsWith('https://')) {
this.options.secure = true
this.options.host = this.options.host.replace(/^https:\/\//, '')
}
}

public build<T extends Service>(
service: new (builder: ServiceBuilder) => T
): T {
this.resolvePotentialSchemaInHost()
return new service(this)
}

public get url() {
return `${this.options.secure ?? true ? 'https' : 'http'}://${
return `http${this.options.secure ? 's' : ''}://${removeTrailingSlashes(
this.options.host
}${this.options.basePath ?? ''}`
)}${removeTrailingSlashes(this.options.basePath) ?? ''}`
}
}
17 changes: 17 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Removes trailing slashes from a string.
*
* @param str - The input string.
* @returns The string with trailing slashes removed.
*/
export const removeTrailingSlashes = (str?: string) =>
str ? str.replace(/(?<![:/])[/]+$/g, '') : undefined

/**
* Adds a leading slash to a string if it doesn't already have one.
*
* @param str - The string to add a leading slash to.
* @returns The string with a leading slash.
*/
export const addLeadingSlash = (str?: string) =>
str ? (str.startsWith('/') ? str : '/' + str) : undefined

0 comments on commit f728ab6

Please sign in to comment.