<commit_before>import { ParsedUrlQueryInput } from 'querystring';
import { addQueryToUrl, appendPathnameToUrl } from 'url-transformers';
import { compactDefined, flow } from './fp';
import { ApiResponse, handleFetchResponse, HandleResponse } from './response';
import { isDefined, OmitStrict } from './typescript';

type BuildUrlParams = {
  pathname: string;
  query?: ParsedUrlQueryInput;
};

const buildUrl = ({ pathname, query = {} }: BuildUrlParams) =>
  flow(appendPathnameToUrl(pathname), addQueryToUrl(compactDefined(query)));

type FetchParams = Pick<RequestInit, 'method'>;
/**
 * The params generated by the library
 */
type BaseRequestParams = BuildUrlParams & FetchParams;

/**
 * Additional fetch options provided by the user on a per-call basis
 */
type AdditionalPerFetchParams = Omit<RequestInit, keyof FetchParams>;
type CompleteRequestParams = BaseRequestParams & AdditionalPerFetchParams;

/**
 * helper used to type-check the arguments, and add default params for all requests
 */
export const createRequestHandler = <Args extends {}>(fn: (a: Args) => BaseRequestParams) => (
  a: Args,
  additionalFetchOptions: AdditionalPerFetchParams = {},
): CompleteRequestParams => ({
  ...fn(a),
  ...additionalFetchOptions,
});

/**
 * Initial parameters that apply to all calls
 */
type InitParams = {
  accessKey?: string;
  apiVersion?: string;
  apiUrl?: string;
} & OmitStrict<RequestInit, 'method' | 'body'>;

type RequestGenerator<RequestArgs extends unknown[], ResponseType> = {
  handleRequest: (...a: RequestArgs) => CompleteRequestParams;
  handleResponse: HandleResponse<ResponseType>;
};

type InitMakeRequest = (
  args: InitParams,
) => <RequestArgs extends unknown[], ResponseType>(
  handlers: RequestGenerator<RequestArgs, ResponseType>,
) => (...a: Parameters<typeof handlers['handleRequest']>) => Promise<ApiResponse<ResponseType>>;
<commit_msg>Replace loose `RequestArgs` type, create `HandleRequest`<commit_after>import { ParsedUrlQueryInput } from 'querystring';
import { addQueryToUrl, appendPathnameToUrl } from 'url-transformers';
import { compactDefined, flow } from './fp';
import { ApiResponse, handleFetchResponse, HandleResponse } from './response';
import { isDefined, OmitStrict } from './typescript';

type BuildUrlParams = {
  pathname: string;
  query?: ParsedUrlQueryInput;
};

const buildUrl = ({ pathname, query = {} }: BuildUrlParams) =>
  flow(appendPathnameToUrl(pathname), addQueryToUrl(compactDefined(query)));

type FetchParams = Pick<RequestInit, 'method'>;
/**
 * The params generated by the library
 */
type BaseRequestParams = BuildUrlParams & FetchParams;

/**
 * Additional fetch options provided by the user on a per-call basis
 */
type AdditionalPerFetchParams = Omit<RequestInit, keyof FetchParams>;
type CompleteRequestParams = BaseRequestParams & AdditionalPerFetchParams;
type HandleRequest<Args> = (
  a: Args,
  additionalFetchOptions: AdditionalPerFetchParams,
) => CompleteRequestParams;

/**
 * helper used to type-check the arguments, and add default params for all requests
 */
export const createRequestHandler = <Args extends {}>(
  fn: (a: Args) => BaseRequestParams,
): HandleRequest<Args> => (a, additionalFetchOptions = {}) => ({
  ...fn(a),
  ...additionalFetchOptions,
});

/**
 * Initial parameters that apply to all calls
 */
type InitParams = {
  accessKey?: string;
  apiVersion?: string;
  apiUrl?: string;
} & OmitStrict<RequestInit, 'method' | 'body'>;

type RequestGenerator<Args, ResponseType> = {
  handleRequest: HandleRequest<Args>;
  handleResponse: HandleResponse<ResponseType>;
};

type InitMakeRequest = (
  args: InitParams,
) => <Args, ResponseType>(
  handlers: RequestGenerator<Args, ResponseType>,
) => (...a: Parameters<typeof handlers['handleRequest']>) => Promise<ApiResponse<ResponseType>>;

{'fraction_non_alphanumeric': 0.12970270981320706, 'fraction_numerical': 0.0, 'mean_word_length': 5.681898066783831, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4702369', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1073, 'n_words': 294}