-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update rext client and publish targets (#355)
* fix(rext-client): fix query params handler to handre arrays * fix: add publish target to schematics package * fix: update @tractr/hapify-update-templates-import-path and fix publish target (#350)
- Loading branch information
Showing
21 changed files
with
378 additions
and
144 deletions.
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './get-concat-value-by-path.helper'; | ||
export * from './is-class.helper'; | ||
export * from './unique-array.helper'; | ||
export * from './url.helper'; |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { formatQueryParameters, getUrl } from './url.helper'; | ||
|
||
describe('Url helpers', () => { | ||
describe('formateQueryParameters', () => { | ||
it('should stringify items in arrays', () => { | ||
const queryParams = { | ||
array: ['10', 10, false, { test: 'test' }], | ||
}; | ||
|
||
const expectedResult = | ||
'array[]=10&array[]=10&array[]=false&array[]=%7B%22test%22%3A%22test%22%7D'; | ||
|
||
const result = formatQueryParameters(queryParams); | ||
|
||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it('should stringify dates', () => { | ||
const date = new Date('August 19, 1975 23:15:30 GMT-3:00'); | ||
|
||
const queryParams = { | ||
date, | ||
}; | ||
|
||
const expectedResult = 'date=1975-08-20T02%3A15%3A30.000Z'; | ||
const result = formatQueryParameters(queryParams); | ||
|
||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it('should stringify objects', () => { | ||
const queryParams = { | ||
object: { test: 'foo' }, | ||
}; | ||
|
||
const expectedResult = 'object=%7B%22test%22%3A%22foo%22%7D'; | ||
|
||
const result = formatQueryParameters(queryParams); | ||
|
||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it('should ignore empty strings and undefined values and keep null', () => { | ||
const queryParams = { | ||
emptyString: '', | ||
nonDefined: undefined, | ||
nullish: null, | ||
}; | ||
|
||
const expectedResult = 'nullish=null'; | ||
|
||
const result = formatQueryParameters(queryParams); | ||
|
||
expect(result).toEqual(expectedResult); | ||
}); | ||
|
||
it('should return valid queryParams', () => { | ||
const date = new Date('August 19, 1975 23:15:30 GMT-3:00'); | ||
|
||
const queryParams = { | ||
nonDefined: undefined, | ||
nullish: null, | ||
array: ['item1', 'item2'], | ||
boolean: true, | ||
date, | ||
number: 100, | ||
object: { test: 'foo' }, | ||
objectArray: [{ test1: 'foo' }, { test2: 'foo' }], | ||
string: 'string', | ||
}; | ||
|
||
const expectedResult = | ||
'array[]=item1&array[]=item2&boolean=true&date=1975-08-20T02%3A15%3A30.000Z&nullish=null&number=100&object=%7B%22test%22%3A%22foo%22%7D&objectArray[]=%7B%22test1%22%3A%22foo%22%7D&objectArray[]=%7B%22test2%22%3A%22foo%22%7D&string=string'; | ||
|
||
const result = formatQueryParameters(queryParams); | ||
|
||
expect(result).toEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('getUrl', () => { | ||
it('should build a correct url', () => { | ||
const baseUrl = new URL('https://base.com'); | ||
const parameters = '/users'; | ||
|
||
const date = new Date('August 19, 1975 23:15:30 GMT-3:00'); | ||
const queryParams = { | ||
nonDefined: undefined, | ||
nullish: null, | ||
array: ['item1', 'item2'], | ||
boolean: true, | ||
date, | ||
number: 100, | ||
object: { test: 'foo' }, | ||
objectArray: [{ test1: 'foo' }, { test2: 'foo' }], | ||
string: 'string', | ||
}; | ||
|
||
const expectedResult = | ||
'https://base.com/users?array[]=item1&array[]=item2&boolean=true&date=1975-08-20T02%3A15%3A30.000Z&nullish=null&number=100&object=%7B%22test%22%3A%22foo%22%7D&objectArray[]=%7B%22test1%22%3A%22foo%22%7D&objectArray[]=%7B%22test2%22%3A%22foo%22%7D&string=string'; | ||
|
||
const result = getUrl(baseUrl, parameters, queryParams).toString(); | ||
|
||
expect(result).toBe(expectedResult); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import * as queryString from 'query-string'; | ||
|
||
/** | ||
* Format a query parameters object into a query parameters string | ||
* @param queryParams - Object to format | ||
* @returns a query parameters string | ||
*/ | ||
export function formatQueryParameters(queryParams: Record<string, any>) { | ||
const formattedQueryParams = Object.entries(queryParams).reduce( | ||
(acc, [key, value]) => { | ||
// for values of type array, items that are of type object are stringified | ||
if (Array.isArray(value)) { | ||
acc[key] = value.map((item) => | ||
typeof item === 'object' ? JSON.stringify(item) : item, | ||
); | ||
|
||
// Dates must be stringified | ||
} else if (value instanceof Date) { | ||
acc[key] = value.toISOString(); | ||
|
||
// Objects must be stringified | ||
} else if (typeof value === 'object') { | ||
acc[key] = JSON.stringify(value); | ||
|
||
// Other types of values does no require any process | ||
} else { | ||
acc[key] = value; | ||
} | ||
|
||
return acc; | ||
}, | ||
{} as Record<string, any>, | ||
); | ||
|
||
return queryString.stringify(formattedQueryParams, { | ||
arrayFormat: 'bracket', | ||
skipNull: false, | ||
skipEmptyString: true, | ||
}); | ||
} | ||
|
||
/** | ||
* Build a correctly formatted url from a base url, | ||
* url parameters and query parameters | ||
* | ||
* @param baseUrl - Base of the url (protocol and domain) | ||
* @param parameters - Url parameters | ||
* @param queryParams - Url query parameters | ||
* @returns a correctly formatted url | ||
*/ | ||
export function getUrl( | ||
baseUrl: URL, | ||
parameters = '', | ||
queryParams: Record<string, any> = {}, | ||
): URL { | ||
const url = new URL( | ||
`${baseUrl.pathname.replace(/\/$/, '')}${parameters}`, | ||
baseUrl, | ||
); | ||
url.search = formatQueryParameters(queryParams); | ||
return url; | ||
} |
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
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
1 change: 0 additions & 1 deletion
1
libs/hapify/templates/rext-client/hapify/generated/rext-client/helpers/index.ts.hpf
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './rest-ajax'; | ||
export * from './transform-and-validate'; | ||
export * from './url'; | ||
|
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
97 changes: 0 additions & 97 deletions
97
libs/hapify/templates/rext-client/hapify/generated/rext-client/helpers/url.ts.hpf
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.