Add pl-api to workspace
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
7
packages/pl-api/lib/utils/types.ts
Normal file
7
packages/pl-api/lib/utils/types.ts
Normal file
@ -0,0 +1,7 @@
|
||||
/**
|
||||
* Resolve a type into a flat POJO interface if it's been wrapped by generics.
|
||||
* https://gleasonator.com/@alex/posts/AWfK4hyppMDCqrT2y8
|
||||
*/
|
||||
type Resolve<T> = Pick<T, keyof T>;
|
||||
|
||||
export type { Resolve };
|
||||
25
packages/pl-api/lib/utils/url.ts
Normal file
25
packages/pl-api/lib/utils/url.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import queryString from 'query-string';
|
||||
|
||||
// Adapted from Axios https://github.com/axios/axios/blob/v1.x/lib/core/buildFullPath.js
|
||||
const isAbsoluteURL = (url: string) => /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
|
||||
|
||||
const combineURLs = (baseURL: string, relativeURL: string) => relativeURL
|
||||
? baseURL.replace(/\/?\/$/, '') + '/' + relativeURL.replace(/^\/+/, '')
|
||||
: baseURL;
|
||||
|
||||
const buildFullPath = (requestedURL: string, baseURL?: string, params?: Record<string, any>) => {
|
||||
const path = (baseURL && !isAbsoluteURL(requestedURL)) ? combineURLs(baseURL, requestedURL) : requestedURL;
|
||||
|
||||
if (params && Object.entries(params).length) {
|
||||
const { url, query } = queryString.parseUrl(path);
|
||||
return `${url}?${queryString.stringify({ ...query, ...params }, { arrayFormat: 'bracket' })}`;
|
||||
}
|
||||
|
||||
return path;
|
||||
};
|
||||
|
||||
export {
|
||||
isAbsoluteURL,
|
||||
combineURLs,
|
||||
buildFullPath,
|
||||
};
|
||||
Reference in New Issue
Block a user