@equinor/fusion-framework-module-http
Advanced tools
Comparing version 0.1.0-beta.0 to 0.1.0-beta.1
@@ -6,2 +6,11 @@ export class ClientNotFoundException extends Error { | ||
} | ||
const isURL = (url) => { | ||
const pattern = new RegExp('^(https?:\\/\\/)?' + | ||
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|' + | ||
'((\\d{1,3}\\.){3}\\d{1,3}))' + | ||
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + | ||
'(\\?[;&a-z\\d%_.~+=-]*)?' + | ||
'(\\#[-a-z\\d_]*)?$', 'i'); | ||
return pattern.test(url); | ||
}; | ||
export class HttpClientProvider { | ||
@@ -16,8 +25,12 @@ config; | ||
createClient(key) { | ||
if (!this.hasClient(key)) { | ||
let config = this.config.clients[key]; | ||
if (!config && isURL(key)) { | ||
config = { baseUri: key }; | ||
} | ||
else { | ||
throw new ClientNotFoundException(`No registered http client for key [${key}]`); | ||
} | ||
const { defaultUri, onCreate, ctor = this.config.defaultHttpClientCtor, requestHandler = this.config.defaultHttpRequestHandler, } = this.config.clients[key]; | ||
const { baseUri, onCreate, ctor = this.config.defaultHttpClientCtor, requestHandler = this.config.defaultHttpRequestHandler, } = config; | ||
const options = { requestHandler }; | ||
const instance = new ctor(defaultUri || '', options); | ||
const instance = new ctor(baseUri || '', options); | ||
onCreate && onCreate(instance); | ||
@@ -24,0 +37,0 @@ return instance; |
@@ -9,3 +9,3 @@ import { HttpRequestHandler, HttpRequestInit, IHttpClient } from './client'; | ||
interface HttpClientOptions<TClient extends IHttpClient> { | ||
defaultUri?: string; | ||
baseUri?: string; | ||
ctor?: HttpClientConstructor<TClient>; | ||
@@ -12,0 +12,0 @@ onCreate?: (client: TClient) => void; |
{ | ||
"name": "@equinor/fusion-framework-module-http", | ||
"version": "0.1.0-beta.0", | ||
"version": "0.1.0-beta.1", | ||
"description": "", | ||
@@ -24,3 +24,3 @@ "main": "./dist/esm/index.js", | ||
"dependencies": { | ||
"@equinor/fusion-framework-module": "^0.1.0-beta.0" | ||
"@equinor/fusion-framework-module": "^0.1.0-beta.1" | ||
}, | ||
@@ -35,3 +35,3 @@ "types": "index.d.ts", | ||
}, | ||
"gitHead": "6fcf14ad456aad818c9e8b3a575b14e2b23ca6b7" | ||
"gitHead": "e1d4d80409b59433fbd00703254e13032ada8680" | ||
} |
@@ -15,3 +15,3 @@ import { HttpRequestHandler, HttpRequestInit, IHttpClient } from './client'; | ||
interface HttpClientOptions<TClient extends IHttpClient> { | ||
defaultUri?: string; | ||
baseUri?: string; | ||
ctor?: HttpClientConstructor<TClient>; | ||
@@ -18,0 +18,0 @@ onCreate?: (client: TClient) => void; |
@@ -27,2 +27,15 @@ import { HttpClient } from './client'; | ||
const isURL = (url: string) => { | ||
const pattern = new RegExp( | ||
'^(https?:\\/\\/)?' + // protocol | ||
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|' + // domain name | ||
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address | ||
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path | ||
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string | ||
'(\\#[-a-z\\d_]*)?$', | ||
'i' | ||
); // fragment locator | ||
return pattern.test(url); | ||
}; | ||
export class HttpClientProvider<TClient extends HttpClient> | ||
@@ -38,13 +51,16 @@ implements IHttpClientProvider<TClient> | ||
public createClient(key: string): TClient { | ||
if (!this.hasClient(key)) { | ||
let config = this.config.clients[key]; | ||
if (!config && isURL(key)) { | ||
config = { baseUri: key }; | ||
} else { | ||
throw new ClientNotFoundException(`No registered http client for key [${key}]`); | ||
} | ||
const { | ||
defaultUri, | ||
baseUri, | ||
onCreate, | ||
ctor = this.config.defaultHttpClientCtor, | ||
requestHandler = this.config.defaultHttpRequestHandler, | ||
} = this.config.clients[key]; | ||
} = config; | ||
const options = { requestHandler }; | ||
const instance = new ctor(defaultUri || '', options) as TClient; | ||
const instance = new ctor(baseUri || '', options) as TClient; | ||
onCreate && onCreate(instance as TClient); | ||
@@ -51,0 +67,0 @@ return instance as TClient; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89395
709