vue-axios-http
Advanced tools
Comparing version 3.2.1 to 3.3.0
@@ -5,2 +5,22 @@ # Changelog | ||
## [3.3.0](https://github.com/chantouchsek/vue-axios-http/compare/v3.2.1...v3.3.0) (2023-05-18) | ||
### Features | ||
* :sparkles: add remove param option ([267afba](https://github.com/chantouchsek/vue-axios-http/commit/267afbac4926f40c97dff4146d4b519da15442b1)) | ||
* :tada: optimize some logic of util ([ad4fd10](https://github.com/chantouchsek/vue-axios-http/commit/ad4fd100d8c218d7682fae42f621893dcb03b021)) | ||
### Bug Fixes | ||
* :bug: correty default value of parameters ([9fc25a1](https://github.com/chantouchsek/vue-axios-http/commit/9fc25a1d3b29a913a3d2c08dff72ebde966c0b22)) | ||
* :pencil2: fix the typos ([39e989c](https://github.com/chantouchsek/vue-axios-http/commit/39e989c208025adb4bd1d804e83b8dc5b38148f0)) | ||
* :penicil2: rename the property ([76bdf91](https://github.com/chantouchsek/vue-axios-http/commit/76bdf910955cd446d19e01435fa5e1c3d0a4cf14)) | ||
* :poop: remove the test ([505bf94](https://github.com/chantouchsek/vue-axios-http/commit/505bf94247d0094b1d3baf7a928eeb8010577f43)) | ||
* **deps:** bump actions/checkout from 3.3.0 to 3.5.2 ([868c4b2](https://github.com/chantouchsek/vue-axios-http/commit/868c4b20a5bfe952ea3fffc01e15956b77da79ea)) | ||
* **deps:** bump actions/stale from 7 to 8 ([a5ea951](https://github.com/chantouchsek/vue-axios-http/commit/a5ea951962e10cdc365e603b9fe60af22e7f13de)) | ||
* **deps:** bump axios from 0.27.2 to 1.3.0 ([cb065a4](https://github.com/chantouchsek/vue-axios-http/commit/cb065a41d41c976953c669d5716dad01d5038c64)) | ||
* **deps:** bump qs from 6.11.0 to 6.11.2 ([c9eaba9](https://github.com/chantouchsek/vue-axios-http/commit/c9eaba9542aefeee7ea8c3eb2a393fe810e2b5e5)) | ||
### [3.2.1](https://github.com/chantouchsek/vue-axios-http/compare/v3.2.0...v3.2.1) (2023-01-26) | ||
@@ -7,0 +27,0 @@ |
/// <reference types="@types/qs" /> | ||
import type { ValidatorType } from './Validator'; | ||
import type { AxiosInstance, Method, AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import type { IParseOptions } from 'qs'; | ||
declare class BaseService { | ||
errors: ValidatorType; | ||
export default class BaseService { | ||
readonly endpoint: string; | ||
parameters: Record<string, any>; | ||
endpoint: string; | ||
errors: import("./Validator").ValidatorType; | ||
static $http: AxiosInstance; | ||
static $errorProperty: string; | ||
static $resetParameter?: boolean | undefined; | ||
static $parsedQs: IParseOptions; | ||
constructor(endpoint: string, parameters: Record<string, any>); | ||
constructor(endpoint: string, parameters?: Record<string, any>); | ||
get $http(): AxiosInstance; | ||
get $errorProperty(): string; | ||
get $resetParameter(): boolean | undefined; | ||
get $parsedQs(): IParseOptions; | ||
@@ -25,9 +26,8 @@ all<T = any>(): Promise<T>; | ||
remove<T = any>(id: string | number): Promise<T>; | ||
submit<T = any>(method: Method, param?: string | number, form?: any, config?: AxiosRequestConfig): Promise<T>; | ||
$submit<T = any>(method: Method, param?: string | number, form?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T, any>>; | ||
$submit<T = any, F = any>(method: Method, param?: string | number, form?: F, config?: AxiosRequestConfig): Promise<AxiosResponse<T, any>>; | ||
submit<T = any, F = any>(method: Method, url?: string | number, form?: F, config?: AxiosRequestConfig): Promise<T>; | ||
private __getParameterString; | ||
private static __validateRequestType; | ||
setParameters(parameters: Record<string, any>): this; | ||
setParameter(parameter: string, value?: any): this; | ||
removeParameters(parameters?: any[]): this; | ||
removeParameters(parameters?: string[]): this; | ||
removeParameter(parameter: string): this; | ||
@@ -38,2 +38,1 @@ onFail(errors: Record<string, any>): void; | ||
} | ||
export default BaseService; |
@@ -13,3 +13,3 @@ "use strict"; | ||
class BaseService { | ||
constructor(endpoint, parameters) { | ||
constructor(endpoint, parameters = {}) { | ||
this.endpoint = endpoint; | ||
@@ -25,2 +25,5 @@ this.parameters = parameters; | ||
} | ||
get $resetParameter() { | ||
return BaseService.$resetParameter; | ||
} | ||
get $parsedQs() { | ||
@@ -45,5 +48,4 @@ return BaseService.$parsedQs; | ||
const requestType = (0, util_1.hasFiles)(body) ? 'post' : 'put'; | ||
if ((0, util_1.hasFiles)(body)) { | ||
if ((0, util_1.hasFiles)(body)) | ||
Object.assign(body, { _method: 'put' }); | ||
} | ||
return this.submit(requestType, parameter, body, config); | ||
@@ -65,18 +67,11 @@ } | ||
} | ||
submit(method, param, form, config) { | ||
return new Promise((resolve, reject) => { | ||
this.$submit(method, param, form, config) | ||
.then(({ data }) => resolve(data)) | ||
.catch((err) => reject(err)); | ||
}); | ||
} | ||
$submit(method, param, form, config) { | ||
BaseService.__validateRequestType(method); | ||
this.beforeSubmit(); | ||
return new Promise((resolve, reject) => { | ||
const data = (0, util_1.hasFiles)(form) ? (0, util_1.objectToFormData)(form) : form; | ||
const endpoint = param ? `/${this.endpoint}/${param}` : `/${this.endpoint}`; | ||
const url = this.__getParameterString((0, util_1.removeDoubleSlash)(endpoint)); | ||
config = Object.assign({}, config, { url, data, method }); | ||
this.$http(config) | ||
const formData = (0, util_1.hasFiles)(form) ? (0, util_1.objectToFormData)(form) : form; | ||
const endpointPath = param ? `/${this.endpoint}/${param}` : `/${this.endpoint}`; | ||
const endpoint = endpointPath.replace(/\/\//g, '/'); | ||
const url = this.__getParameterString(endpoint); | ||
const axiosConfig = { url, data: formData, method, ...config }; | ||
this.$http(axiosConfig) | ||
.then((response) => { | ||
@@ -92,40 +87,23 @@ this.onSuccess(); | ||
const { data } = response; | ||
const errors = {}; | ||
Object.assign(errors, data[this.$errorProperty]); | ||
this.onFail(errors); | ||
const validationErrors = {}; | ||
Object.assign(validationErrors, data[this.$errorProperty]); | ||
this.onFail(validationErrors); | ||
} | ||
reject(error); | ||
}); | ||
if (this.$resetParameter) | ||
this.removeParameters(); | ||
}); | ||
} | ||
submit(method, url, form, config) { | ||
return new Promise((resolve, reject) => { | ||
this.$submit(method, url, form, config) | ||
.then(({ data }) => resolve(data)) | ||
.catch((err) => reject(err)); | ||
}); | ||
} | ||
__getParameterString(url) { | ||
const query = qs_1.default.stringify(this.parameters, { | ||
encode: false, | ||
skipNulls: true, | ||
addQueryPrefix: true, | ||
}); | ||
const query = qs_1.default.stringify(this.parameters, { encode: false, skipNulls: true, addQueryPrefix: true }); | ||
return `${url}${query}`; | ||
} | ||
static __validateRequestType(requestType) { | ||
const requestTypes = [ | ||
'get', | ||
'GET', | ||
'delete', | ||
'DELETE', | ||
'head', | ||
'HEAD', | ||
'options', | ||
'OPTIONS', | ||
'post', | ||
'POST', | ||
'put', | ||
'PUT', | ||
'patch', | ||
'PATCH', | ||
]; | ||
if (!requestTypes.includes(requestType)) { | ||
throw new Error(`\`${requestType}\` is not a valid request type, ` + `must be one of: \`${requestTypes.join('`, `')}\`.`); | ||
} | ||
return requestType; | ||
} | ||
setParameters(parameters) { | ||
@@ -152,8 +130,7 @@ Object.keys(parameters).forEach((key) => { | ||
if (!parameters || !parameters.length) { | ||
this.parameters = []; | ||
this.parameters = {}; | ||
} | ||
else if ((0, lodash_1.isArray)(parameters)) { | ||
for (const parameter of parameters) { | ||
else if (Array.isArray(parameters)) { | ||
for (const parameter of parameters) | ||
delete this.parameters[parameter]; | ||
} | ||
} | ||
@@ -171,5 +148,4 @@ return this; | ||
beforeSubmit() { | ||
if (!this.$http) { | ||
if (!this.$http) | ||
throw new Error('Vue Axios Http, No http library provided.'); | ||
} | ||
this.errors.flush(); | ||
@@ -189,3 +165,5 @@ this.errors.processing = true; | ||
} | ||
exports.default = BaseService; | ||
BaseService.$errorProperty = 'errors'; | ||
BaseService.$resetParameter = false; | ||
BaseService.$parsedQs = { | ||
@@ -196,2 +174,1 @@ comma: true, | ||
}; | ||
exports.default = BaseService; |
@@ -7,13 +7,11 @@ "use strict"; | ||
constructor(errors = {}) { | ||
this.errors = errors; | ||
this.processing = false; | ||
this.successful = false; | ||
this.errors = errors; | ||
} | ||
add(field, message, forceUpdate) { | ||
if (this.missed(field)) { | ||
if (this.missed(field)) | ||
this.errors[field] = []; | ||
} | ||
if (!this.errors[field].includes(message)) { | ||
if (!this.errors[field].includes(message)) | ||
this.errors[field].unshift(message); | ||
} | ||
if (forceUpdate) { | ||
@@ -79,5 +77,4 @@ this.errors[field] = []; | ||
} | ||
if (!fields.length) { | ||
if (!fields.length) | ||
return Object.keys(this.errors).length > 0; | ||
} | ||
const errors = {}; | ||
@@ -84,0 +81,0 @@ fields.forEach((key) => (errors[key] = this.get(key))); |
@@ -8,11 +8,15 @@ /// <reference types="@types/qs" /> | ||
import './vue'; | ||
export type { ValidatorType }; | ||
export declare class AxiosHttp { | ||
installed: boolean; | ||
interface ModuleOptions { | ||
resetParameter?: boolean; | ||
parsedQs: IParseOptions; | ||
install(Vue: typeof _Vue, options?: Record<string, any>): void; | ||
errorProperty?: string | 'errors' | 'message'; | ||
} | ||
declare class AxiosHttp { | ||
installed: boolean; | ||
install(Vue: typeof _Vue, options?: Partial<ModuleOptions>): void; | ||
} | ||
export * from './util'; | ||
export type { ValidatorType, ModuleOptions }; | ||
export { Validator, BaseService }; | ||
export * from './util'; | ||
declare const _default: AxiosHttp; | ||
export default _default; |
@@ -20,3 +20,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.BaseService = exports.Validator = exports.AxiosHttp = void 0; | ||
exports.BaseService = exports.Validator = void 0; | ||
const lodash_1 = require("lodash"); | ||
@@ -29,10 +29,14 @@ const BaseService_1 = __importDefault(require("./core/BaseService")); | ||
require("./vue"); | ||
const optionDefault = { | ||
resetParameter: false, | ||
parsedQs: { | ||
comma: true, | ||
allowDots: true, | ||
ignoreQueryPrefix: true, | ||
}, | ||
errorProperty: 'errors', | ||
}; | ||
class AxiosHttp { | ||
constructor() { | ||
this.installed = false; | ||
this.parsedQs = { | ||
comma: true, | ||
allowDots: true, | ||
ignoreQueryPrefix: true, | ||
}; | ||
} | ||
@@ -43,7 +47,6 @@ install(Vue, options = {}) { | ||
this.installed = true; | ||
const defaultOption = (0, lodash_1.merge)({ parsedQs: this.parsedQs, errorProperty: 'errors' }, options); | ||
const { $axios, errorProperty, parsedQs } = defaultOption; | ||
BaseService_1.default.$http = $axios; | ||
const { errorProperty, parsedQs, resetParameter } = (0, lodash_1.merge)(optionDefault, options); | ||
BaseService_1.default.$parsedQs = parsedQs; | ||
BaseService_1.default.$resetParameter = resetParameter; | ||
BaseService_1.default.$errorProperty = errorProperty || 'errors'; | ||
BaseService_1.default.$parsedQs = parsedQs || this.parsedQs; | ||
Vue.mixin({ | ||
@@ -66,4 +69,3 @@ beforeCreate() { | ||
} | ||
exports.AxiosHttp = AxiosHttp; | ||
__exportStar(require("./util"), exports); | ||
exports.default = new AxiosHttp(); |
@@ -1,3 +0,3 @@ | ||
export declare function objectToFormData(object: any, formData?: FormData, parent?: string): FormData; | ||
export declare function objectToFormData(obj: any, formData?: FormData, parent?: string): FormData; | ||
export declare function hasFilesDeep(obj: any): boolean; | ||
export declare function hasFiles(form: any): boolean; |
@@ -5,11 +5,11 @@ "use strict"; | ||
const objects_1 = require("./objects"); | ||
function objectToFormData(object, formData = new FormData(), parent = '') { | ||
if (object === null || object === 'undefined' || object.length === 0) { | ||
formData.append(parent, object); | ||
function objectToFormData(obj, formData = new FormData(), parent = '') { | ||
if (obj == null || (Array.isArray(obj) && obj.length === 0)) { | ||
formData.append(parent, obj); | ||
} | ||
else { | ||
for (const property in object) { | ||
if ((0, objects_1.hasOwnProperty)(object, property)) { | ||
appendToFormData(formData, getKey(parent, property), object[property]); | ||
} | ||
const propertyMap = new Map(Object.entries(obj)); | ||
for (const [property, value] of propertyMap) { | ||
const key = parent ? `${parent}[${property}]` : property; | ||
appendToFormData(formData, key, value); | ||
} | ||
@@ -20,22 +20,14 @@ } | ||
exports.objectToFormData = objectToFormData; | ||
function getKey(parent, property) { | ||
return parent ? parent + '[' + property + ']' : property; | ||
} | ||
function appendToFormData(formData, key, value) { | ||
if (value instanceof Date) { | ||
if (value instanceof Date) | ||
return formData.append(key, value.toISOString()); | ||
} | ||
if (value instanceof File) { | ||
if (value instanceof File) | ||
return formData.append(key, value, value.name); | ||
} | ||
if (typeof value === 'boolean') { | ||
if (typeof value === 'boolean') | ||
return formData.append(key, value ? '1' : '0'); | ||
} | ||
if (value === null) { | ||
if (value === null) | ||
return formData.append(key, ''); | ||
} | ||
if (typeof value !== 'object') { | ||
if (typeof value !== 'object') | ||
return formData.append(key, value); | ||
} | ||
if ((0, objects_1.isArray)(value) && hasFilesDeep(value)) { | ||
if (Array.isArray(value) && hasFilesDeep(value)) { | ||
for (let i = 0; i < value.length; i++) { | ||
@@ -49,19 +41,14 @@ formData.append(key + '[' + i + ']', value[i], value[i].name); | ||
function hasFilesDeep(obj) { | ||
if (obj === null) | ||
if (!obj) | ||
return false; | ||
if (typeof obj === 'object') { | ||
for (const key in obj) { | ||
if ((0, objects_1.hasOwnProperty)(obj, key) && (0, objects_1.isFile)(obj[key])) | ||
return true; | ||
} | ||
const objValues = Object.values(obj); | ||
if (objValues.some(objects_1.isFile)) | ||
return true; | ||
} | ||
if ((0, objects_1.isArray)(obj)) { | ||
let f = ''; | ||
for (const key in obj) { | ||
if ((0, objects_1.hasOwnProperty)(obj, key)) { | ||
f = key; | ||
break; | ||
} | ||
if (Array.isArray(obj)) { | ||
const nonNullElement = obj.find((el) => el !== null); | ||
if (nonNullElement) { | ||
return hasFilesDeep(nonNullElement); | ||
} | ||
return hasFilesDeep(obj[f]); | ||
} | ||
@@ -73,4 +60,3 @@ return (0, objects_1.isFile)(obj); | ||
for (const prop in form) { | ||
const hasProp = (0, objects_1.hasOwnProperty)(form, prop) || typeof window !== 'undefined'; | ||
if (hasProp && hasFilesDeep(form[prop])) { | ||
if (Object.prototype.hasOwnProperty.call(form, prop) && hasFilesDeep(form[prop])) { | ||
return true; | ||
@@ -77,0 +63,0 @@ } |
@@ -1,4 +0,2 @@ | ||
export declare function isArray(object: any): boolean; | ||
export declare function hasOwnProperty(obj: any, key: any): boolean; | ||
export declare function isFile(object: any): boolean; | ||
export declare function is(errors: any, error: any): boolean; | ||
export declare function is(errors: string[], errorsToCheck: string[] | string): boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.is = exports.isFile = exports.hasOwnProperty = exports.isArray = void 0; | ||
function isArray(object) { | ||
return Object.prototype.toString.call(object) === '[object Array]'; | ||
} | ||
exports.isArray = isArray; | ||
function hasOwnProperty(obj, key) { | ||
if (!obj || !key) | ||
return false; | ||
return Object.prototype.hasOwnProperty.call(obj, key); | ||
} | ||
exports.hasOwnProperty = hasOwnProperty; | ||
exports.is = exports.isFile = void 0; | ||
function isFile(object) { | ||
if (typeof window === 'undefined' || typeof File !== 'function' || typeof FileList !== 'function') { | ||
return false; | ||
} | ||
return object instanceof File || object instanceof FileList; | ||
} | ||
exports.isFile = isFile; | ||
function is(errors, error) { | ||
return isArray(error) ? error.some((w) => is(errors, w)) : errors.includes(error); | ||
function is(errors, errorsToCheck) { | ||
return Array.isArray(errorsToCheck) ? errorsToCheck.some((w) => is(errors, w)) : errors.includes(errorsToCheck); | ||
} | ||
exports.is = is; |
import BaseService from '../core/BaseService'; | ||
declare class PostService extends BaseService { | ||
constructor(parameters?: {}); | ||
tags<T>(id: string | number): Promise<T>; | ||
throwException<T>(id: string | number): Promise<T>; | ||
tags<T = any>(id: string | number): Promise<T>; | ||
} | ||
export default PostService; |
@@ -14,6 +14,3 @@ "use strict"; | ||
} | ||
throwException(id) { | ||
return this.submit('unlink', `${id}/tags`); | ||
} | ||
} | ||
exports.default = PostService; |
@@ -1,3 +0,2 @@ | ||
export declare function removeDoubleSlash(url: string): string; | ||
export declare const toCamelCase: (e: string) => string; | ||
export declare const toSnakeCase: (e: string) => string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toSnakeCase = exports.toCamelCase = exports.removeDoubleSlash = void 0; | ||
function removeDoubleSlash(url) { | ||
return url.replace(/\/\//g, '/'); | ||
} | ||
exports.removeDoubleSlash = removeDoubleSlash; | ||
const toCamelCase = (e) => { | ||
return e.replace(/_([a-z])/g, (g) => g[1].toUpperCase()); | ||
}; | ||
exports.toSnakeCase = exports.toCamelCase = void 0; | ||
const toCamelCase = (e) => e.replace(/_([a-z])/g, (g) => g[1].toUpperCase()); | ||
exports.toCamelCase = toCamelCase; | ||
const toSnakeCase = (e) => { | ||
if (!e.match(/([A-Z])/g)) | ||
return e; | ||
return e.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`); | ||
}; | ||
const toSnakeCase = (e) => (e.match(/([A-Z])/g) ? e.replace(/[A-Z]/g, (l) => `_${l.toLowerCase()}`) : e); | ||
exports.toSnakeCase = toSnakeCase; |
{ | ||
"name": "vue-axios-http", | ||
"version": "3.2.1", | ||
"version": "3.3.0", | ||
"description": "Elegant and simple way to build requests for REST API", | ||
@@ -54,3 +54,3 @@ "main": "dist/index.js", | ||
"@types/lodash": "^4.14.182", | ||
"@types/node": "^18.0.0", | ||
"@types/node": "^20.2.0", | ||
"@types/qs": "^6.9.7", | ||
@@ -57,0 +57,0 @@ "@typescript-eslint/eslint-plugin": "^5.49.0", |
@@ -15,3 +15,3 @@ # Vue Axios Http | ||
meant to be used with a Laravel back end, and it doesn't limit that you need only to work with laravel, Ruby on Rail, | ||
NodeJs, ExpressJs, or any other languages. | ||
Node.js, Express.js, or any other languages. | ||
@@ -51,6 +51,6 @@ Take a look at the [usage section](#usage) to view a detailed example on how to use it. | ||
// With options | ||
['vue-axios-http/nuxt', { errorProperty: 'errors' }], | ||
['vue-axios-http/nuxt', { errorProperty: 'errors', resetParameter: true }], | ||
'@nuxtjs/axios', | ||
], | ||
axiosHttp: { errorProperty: 'errors' }, | ||
axiosHttp: { errorProperty: 'errors', resetParameter: true }, | ||
} | ||
@@ -61,3 +61,3 @@ ``` | ||
you can overwrite it, by adding in config above. | ||
you can overwrite it by adding in the config above. | ||
@@ -100,3 +100,3 @@ ### Note: | ||
Error response must look like: or base on **errorProperty** from config | ||
Error response must look like: or based on **errorProperty** from config | ||
@@ -144,3 +144,3 @@ ```json | ||
## Using with Vuex | ||
## Using it with Vuex | ||
@@ -304,3 +304,3 @@ 1.Create **proxies** folder or your prefer folder name for this | ||
## Service's methods are available | ||
## Service methods are available | ||
@@ -319,3 +319,3 @@ | Method | Description | | ||
**Note**: If you to pass query string as object that can be response like object format at api side. | ||
**Note**: If you to pass query string, as an object that can be response like object format at api side. | ||
@@ -345,3 +345,3 @@ #### Example | ||
**Note**: Query object above will transform into query string like: | ||
**Note**: A query object above will transform into query string like: | ||
@@ -352,3 +352,3 @@ ```text | ||
if setParameter that value is empty or null it will remove that param for query string | ||
if setParameter that value is empty or null, it will remove that param for query string | ||
@@ -438,3 +438,3 @@ #### setParameter() | ||
## How to use in vue component | ||
## How to use in a vue component | ||
@@ -441,0 +441,0 @@ ```vue |
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
56170
652