Skip to content

Commit

Permalink
upgrade axios and copy buildURL
Browse files Browse the repository at this point in the history
  • Loading branch information
SchroterQuentin committed Jun 24, 2024
1 parent 7290c07 commit eb6c586
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 23 deletions.
34 changes: 24 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/Bones.UI/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import "./shims-plugin.d.ts"
import "./shims-axios.d.ts"

export * from "./abstractions"
export * from "./composables"
Expand Down
4 changes: 2 additions & 2 deletions src/Bones.UI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"author": "",
"license": "ISC",
"sideEffects": false,
"dependencies": {
"peerDependencies": {
"@types/lodash": "^4.14.197",
"ajv": "^8.11.0",
"ajv-dist": "^8.11.0",
"axios": "^0.21.1",
"axios": "^1.7.2",
"vue": "^3.2.0",
"lodash": "^4.17.21"
}
Expand Down
4 changes: 0 additions & 4 deletions src/Bones.UI/shims-axios.d.ts

This file was deleted.

111 changes: 111 additions & 0 deletions src/Bones.UI/tools/buildURL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
function encode(val: string) {
return encodeURIComponent(val).
replace(/%3A/gi, ':').
replace(/%24/g, '$').
replace(/%2C/gi, ',').
replace(/%20/g, '+').
replace(/%5B/gi, '[').
replace(/%5D/gi, ']');
}

var toString = Object.prototype.toString;

function forEach(obj: any, fn: any) {
// Don't bother if no value provided
if (obj === null || typeof obj === 'undefined') {
return;
}

// Force an array if not already something iterable
if (typeof obj !== 'object') {
/*eslint no-param-reassign:0*/
obj = [obj];
}

if (isArray(obj)) {
// Iterate over array values
for (var i = 0, l = obj.length; i < l; i++) {
fn.call(null, obj[i], i, obj);
}
} else {
// Iterate over object keys
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
fn.call(null, obj[key], key, obj);
}
}
}
}

function isURLSearchParams(val: any) {
return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
}

function isArray(val: any) {
return toString.call(val) === '[object Array]';
}

function isDate(val: any) {
return toString.call(val) === '[object Date]';
}

function isObject(val: any) {
return val !== null && typeof val === 'object';
}

/**
* Build a URL by appending params to the end
*
* @param {string} url The base of the url (e.g., http://www.google.com)
* @param {object} [params] The params to be appended
* @returns {string} The formatted url
*/
export function buildURL(url: string, params?: any, paramsSerializer?: any) {
/*eslint no-param-reassign:0*/
if (!params) {
return url;
}

var serializedParams;
if (paramsSerializer) {
serializedParams = paramsSerializer(params);
} else if (isURLSearchParams(params)) {
serializedParams = params.toString();
} else {
var parts : string[] = [];

forEach(params, function serialize(val: any, key: any) {
if (val === null || typeof val === 'undefined') {
return;
}

if (isArray(val)) {
key = key + '[]';
} else {
val = [val];
}

forEach(val, function parseValue(v: any) {
if (isDate(v)) {
v = v.toISOString();
} else if (isObject(v)) {
v = JSON.stringify(v);
}
parts.push(encode(key) + '=' + encode(v));
});
});

serializedParams = parts.join('&');
}

if (serializedParams) {
var hashmarkIndex = url.indexOf('#');
if (hashmarkIndex !== -1) {
url = url.slice(0, hashmarkIndex);
}

url += (url.indexOf('?') === -1 ? '?' : '&') + serializedParams;
}

return url;
};
2 changes: 1 addition & 1 deletion src/Bones.UI/tools/urlTools.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import _ from "lodash";
import axiosBuildURL from 'axios/lib/helpers/buildURL';
import { buildURL as axiosBuildURL } from "./buildURL";

function deepFlat(key: string, value: any): { key: string, value: any }[] {
if ((typeof value !== 'object' || value === null || value instanceof Date)) {
Expand Down
Empty file added src/Bones.UI/tools/utils.ts
Empty file.
9 changes: 8 additions & 1 deletion tests/Bones.UI.Tests/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"name": "bones-ui-tests",
"devDependencies": {
"@dative-gpi/bones-ui": "1.0.0",
"@types/jest": "^29.5.5",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"@types/lodash": "^4.14.197",
"ajv": "^8.11.0",
"ajv-dist": "^8.11.0",
"axios": "^1.7.2",
"vue": "^3.2.0",
"lodash": "^4.17.21"
},
"scripts": {
"test": "jest"
Expand Down
4 changes: 3 additions & 1 deletion tests/Bones.UI.Tests/tests/composable.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/**
* @jest-environment jsdom
*/

import * as all from '@dative-gpi/bones-ui';
import { ServiceFactory } from '@dative-gpi/bones-ui';
import { useTestUser, useTestUsers, TEST_USERS_URL, TEST_USER_URL, useCreateTestUser, useUpdateTestUser, useRemoveTestUser } from '../services/testUserService';
import MockAdapter from 'axios-mock-adapter';
import buildURL from 'axios/lib/helpers/buildURL';

import { buildURL } from '@dative-gpi/bones-ui/tools/buildURL';

const mock = new MockAdapter(ServiceFactory.http);

Expand Down
7 changes: 4 additions & 3 deletions tests/Bones.UI.Tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"compilerOptions": {
"baseUrl": ".",
"target": "ESNext",
"target": "ES6",
"useDefineForClassFields": true,
"module": "ESNext",
"module": "ES6",
"moduleResolution": "Node",
"strict": true,
"jsx": "preserve",
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"lib": ["ESNext", "DOM"],
"allowSyntheticDefaultImports": true,
"lib": ["ES6", "DOM"],
"skipLibCheck": true,
"noEmit": true,
"paths": {
Expand Down

0 comments on commit eb6c586

Please sign in to comment.