mirror of
				https://github.com/cp6/my-idlers.git
				synced 2025-11-03 23:59:09 +00:00 
			
		
		
		
	
		
			
	
	
		
			79 lines
		
	
	
	
		
			3.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
		
		
			
		
	
	
			79 lines
		
	
	
	
		
			3.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 
								 | 
							
								"use strict";
							 | 
						||
| 
								 | 
							
								Object.defineProperty(exports, "__esModule", { value: true });
							 | 
						||
| 
								 | 
							
								const is_1 = require("@sindresorhus/is");
							 | 
						||
| 
								 | 
							
								const normalizeArguments = (options, defaults) => {
							 | 
						||
| 
								 | 
							
								    if (is_1.default.null_(options.encoding)) {
							 | 
						||
| 
								 | 
							
								        throw new TypeError('To get a Buffer, set `options.responseType` to `buffer` instead');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    is_1.assert.any([is_1.default.string, is_1.default.undefined], options.encoding);
							 | 
						||
| 
								 | 
							
								    is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.resolveBodyOnly);
							 | 
						||
| 
								 | 
							
								    is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.methodRewriting);
							 | 
						||
| 
								 | 
							
								    is_1.assert.any([is_1.default.boolean, is_1.default.undefined], options.isStream);
							 | 
						||
| 
								 | 
							
								    is_1.assert.any([is_1.default.string, is_1.default.undefined], options.responseType);
							 | 
						||
| 
								 | 
							
								    // `options.responseType`
							 | 
						||
| 
								 | 
							
								    if (options.responseType === undefined) {
							 | 
						||
| 
								 | 
							
								        options.responseType = 'text';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    // `options.retry`
							 | 
						||
| 
								 | 
							
								    const { retry } = options;
							 | 
						||
| 
								 | 
							
								    if (defaults) {
							 | 
						||
| 
								 | 
							
								        options.retry = { ...defaults.retry };
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    else {
							 | 
						||
| 
								 | 
							
								        options.retry = {
							 | 
						||
| 
								 | 
							
								            calculateDelay: retryObject => retryObject.computedValue,
							 | 
						||
| 
								 | 
							
								            limit: 0,
							 | 
						||
| 
								 | 
							
								            methods: [],
							 | 
						||
| 
								 | 
							
								            statusCodes: [],
							 | 
						||
| 
								 | 
							
								            errorCodes: [],
							 | 
						||
| 
								 | 
							
								            maxRetryAfter: undefined
							 | 
						||
| 
								 | 
							
								        };
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    if (is_1.default.object(retry)) {
							 | 
						||
| 
								 | 
							
								        options.retry = {
							 | 
						||
| 
								 | 
							
								            ...options.retry,
							 | 
						||
| 
								 | 
							
								            ...retry
							 | 
						||
| 
								 | 
							
								        };
							 | 
						||
| 
								 | 
							
								        options.retry.methods = [...new Set(options.retry.methods.map(method => method.toUpperCase()))];
							 | 
						||
| 
								 | 
							
								        options.retry.statusCodes = [...new Set(options.retry.statusCodes)];
							 | 
						||
| 
								 | 
							
								        options.retry.errorCodes = [...new Set(options.retry.errorCodes)];
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    else if (is_1.default.number(retry)) {
							 | 
						||
| 
								 | 
							
								        options.retry.limit = retry;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    if (is_1.default.undefined(options.retry.maxRetryAfter)) {
							 | 
						||
| 
								 | 
							
								        options.retry.maxRetryAfter = Math.min(
							 | 
						||
| 
								 | 
							
								        // TypeScript is not smart enough to handle `.filter(x => is.number(x))`.
							 | 
						||
| 
								 | 
							
								        // eslint-disable-next-line unicorn/no-fn-reference-in-iterator
							 | 
						||
| 
								 | 
							
								        ...[options.timeout.request, options.timeout.connect].filter(is_1.default.number));
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    // `options.pagination`
							 | 
						||
| 
								 | 
							
								    if (is_1.default.object(options.pagination)) {
							 | 
						||
| 
								 | 
							
								        if (defaults) {
							 | 
						||
| 
								 | 
							
								            options.pagination = {
							 | 
						||
| 
								 | 
							
								                ...defaults.pagination,
							 | 
						||
| 
								 | 
							
								                ...options.pagination
							 | 
						||
| 
								 | 
							
								            };
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        const { pagination } = options;
							 | 
						||
| 
								 | 
							
								        if (!is_1.default.function_(pagination.transform)) {
							 | 
						||
| 
								 | 
							
								            throw new Error('`options.pagination.transform` must be implemented');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if (!is_1.default.function_(pagination.shouldContinue)) {
							 | 
						||
| 
								 | 
							
								            throw new Error('`options.pagination.shouldContinue` must be implemented');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if (!is_1.default.function_(pagination.filter)) {
							 | 
						||
| 
								 | 
							
								            throw new TypeError('`options.pagination.filter` must be implemented');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        if (!is_1.default.function_(pagination.paginate)) {
							 | 
						||
| 
								 | 
							
								            throw new Error('`options.pagination.paginate` must be implemented');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    // JSON mode
							 | 
						||
| 
								 | 
							
								    if (options.responseType === 'json' && options.headers.accept === undefined) {
							 | 
						||
| 
								 | 
							
								        options.headers.accept = 'application/json';
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								    return options;
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								exports.default = normalizeArguments;
							 |