@mittwald/api-client-commons 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ Common code base used by `@mittwald/api-client-*` package.
@@ -0,0 +1 @@
1
+ export * from "axios";
package/dist/axios.js ADDED
@@ -0,0 +1 @@
1
+ export * from "axios";
@@ -0,0 +1,9 @@
1
+ import { AxiosInstance, CreateAxiosDefaults } from "axios";
2
+ import { RequestFunction } from "../types/RequestFunction.js";
3
+ import { OpenAPIOperation } from "../types/OpenAPIOperation.js";
4
+ export declare abstract class ApiClientBase {
5
+ axios: AxiosInstance;
6
+ constructor(axiosConfig?: AxiosInstance | CreateAxiosDefaults);
7
+ protected requestFunctionFactory<TOp extends OpenAPIOperation>(operation: TOp): RequestFunction<TOp>;
8
+ }
9
+ export default ApiClientBase;
@@ -0,0 +1,13 @@
1
+ import axios, { Axios } from "axios";
2
+ import Request from "./Request.js";
3
+ export class ApiClientBase {
4
+ axios;
5
+ constructor(axiosConfig = axios) {
6
+ this.axios =
7
+ axiosConfig instanceof Axios ? axiosConfig : axios.create(axiosConfig);
8
+ }
9
+ requestFunctionFactory(operation) {
10
+ return (conf) => new Request(this.axios, operation, conf).execute();
11
+ }
12
+ }
13
+ export default ApiClientBase;
@@ -0,0 +1,7 @@
1
+ import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from "axios";
2
+ import { AnyResponse } from "../types/Response.js";
3
+ export declare class ApiClientError<T = unknown, D = unknown> extends AxiosError<T, D> {
4
+ constructor(message?: string, code?: string, config?: InternalAxiosRequestConfig<D>, request?: unknown, response?: AxiosResponse<T, D>);
5
+ static fromResponse(message: string, response: AnyResponse): ApiClientError;
6
+ }
7
+ export default ApiClientError;
@@ -0,0 +1,12 @@
1
+ import { AxiosError } from "axios";
2
+ export class ApiClientError extends AxiosError {
3
+ constructor(message, code, config, request, response) {
4
+ super(message, code, config, request, response);
5
+ Object.setPrototypeOf(this, ApiClientError.prototype);
6
+ this.name = "ApiClientError";
7
+ }
8
+ static fromResponse(message, response) {
9
+ return new ApiClientError(message, undefined, response.config, response.request, response);
10
+ }
11
+ }
12
+ export default ApiClientError;
@@ -0,0 +1,9 @@
1
+ import { PathParameters } from "../types/http.js";
2
+ export declare class OpenAPIPath {
3
+ private readonly rawPath;
4
+ private readonly params?;
5
+ constructor(rawPath: string, params?: PathParameters);
6
+ buildUrl(): string;
7
+ private static setPathParams;
8
+ }
9
+ export default OpenAPIPath;
@@ -0,0 +1,20 @@
1
+ export class OpenAPIPath {
2
+ rawPath;
3
+ params;
4
+ constructor(rawPath, params) {
5
+ this.rawPath = rawPath;
6
+ this.params = params;
7
+ }
8
+ buildUrl() {
9
+ return OpenAPIPath.setPathParams(this.rawPath, this.params);
10
+ }
11
+ static setPathParams(path, params) {
12
+ const asEntries = Object.entries(params ?? {});
13
+ const finalPath = asEntries.reduce((path, entry) => {
14
+ const [key, value] = entry;
15
+ return path.replace(`{${key}}`, encodeURIComponent(value));
16
+ }, path);
17
+ return finalPath.startsWith("/") ? finalPath.substring(1) : finalPath;
18
+ }
19
+ }
20
+ export default OpenAPIPath;
@@ -0,0 +1,12 @@
1
+ import { OpenAPIOperation } from "../types/OpenAPIOperation.js";
2
+ import { RequestConfig, ResponsePromise } from "../types/RequestFunction.js";
3
+ import { AxiosInstance } from "axios";
4
+ export declare class Request<TOp extends OpenAPIOperation> {
5
+ private readonly axios;
6
+ private readonly operationDescriptor;
7
+ private readonly config?;
8
+ constructor(axiosInstance: AxiosInstance, operationDescriptor: TOp, config?: RequestConfig<TOp>);
9
+ execute(): ResponsePromise<TOp>;
10
+ private buildAxiosConfig;
11
+ }
12
+ export default Request;
@@ -0,0 +1,33 @@
1
+ import OpenAPIPath from "./OpenAPIPath.js";
2
+ export class Request {
3
+ axios;
4
+ operationDescriptor;
5
+ config;
6
+ constructor(axiosInstance, operationDescriptor, config) {
7
+ this.axios = axiosInstance;
8
+ this.operationDescriptor = operationDescriptor;
9
+ this.config = config;
10
+ }
11
+ execute() {
12
+ return this.axios.request(this.buildAxiosConfig());
13
+ }
14
+ buildAxiosConfig() {
15
+ const { method, path } = this.operationDescriptor;
16
+ const pathParameters = this.config &&
17
+ "pathParameters" in this.config &&
18
+ this.config.pathParameters !== null
19
+ ? this.config.pathParameters
20
+ : undefined;
21
+ const openApiPath = new OpenAPIPath(path, pathParameters);
22
+ const data = this.config && "data" in this.config ? this.config.data : undefined;
23
+ const headers = this.config && "headers" in this.config ? this.config.headers : undefined;
24
+ return {
25
+ url: openApiPath.buildUrl(),
26
+ method,
27
+ headers,
28
+ data,
29
+ validateStatus: () => true,
30
+ };
31
+ }
32
+ }
33
+ export default Request;
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export * from "type-fest";
@@ -0,0 +1 @@
1
+ export * from "type-fest";
@@ -0,0 +1,6 @@
1
+ import { HasRequiredKeys, PartialOnUndefinedDeep } from "type-fest";
2
+ type PartialOnNoRequiredKeysDeep<T> = PartialOnUndefinedDeep<{
3
+ [TKey in keyof T]: HasRequiredKeys<PartialOnNoRequiredKeysDeep<T[TKey]>> extends true ? T[TKey] : T[TKey] | undefined;
4
+ }>;
5
+ export type NullableOnNoRequiredKeysDeep<T> = HasRequiredKeys<PartialOnNoRequiredKeysDeep<T>> extends true ? PartialOnNoRequiredKeysDeep<T> : PartialOnNoRequiredKeysDeep<T> | null;
6
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { AnyResponse, Response } from "./Response.js";
2
+ import { AnyRequest, Request } from "./Request.js";
3
+ import { HttpMethod } from "./http.js";
4
+ export interface OpenAPIOperation<TIgnoredRequest extends AnyRequest = Request, IgnoredResponse extends AnyResponse = Response> {
5
+ operationId: string;
6
+ path: string;
7
+ method: HttpMethod;
8
+ }
9
+ export type InferredRequestType<TOp> = TOp extends OpenAPIOperation<infer TReq> ? TReq : never;
10
+ export type InferredResponseType<TOp> = TOp extends OpenAPIOperation<Request, infer TRes> ? TRes : never;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,14 @@
1
+ import { HttpHeaders, HttpPayload, PathParameters } from "./http.js";
2
+ type RequestWithData<TData> = TData extends null ? object : {
3
+ data: TData;
4
+ };
5
+ type RequestWithPathParameters<TPathParameters> = TPathParameters extends null ? object : {
6
+ pathParameters: TPathParameters;
7
+ };
8
+ type RequestWithHeaders<THeaders> = THeaders extends null ? object : {
9
+ headers: THeaders;
10
+ };
11
+ type EmptyRequest = Record<string, never>;
12
+ export type Request<TData extends HttpPayload = null, TPathParameters extends PathParameters | null = null, THeader extends HttpHeaders | null = null> = TData | TPathParameters | THeader extends null ? EmptyRequest : RequestWithData<TData> & RequestWithPathParameters<TPathParameters> & RequestWithHeaders<THeader>;
13
+ export type AnyRequest = Request<any, any, any>;
14
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { OpenAPIOperation, InferredRequestType, InferredResponseType } from "./OpenAPIOperation.js";
2
+ import { NullableOnNoRequiredKeysDeep } from "./NullableOnNoRequiredKeysDeep.js";
3
+ export type RequestConfig<TOp extends OpenAPIOperation> = NullableOnNoRequiredKeysDeep<InferredRequestType<TOp>>;
4
+ export type ResponsePromise<TOp extends OpenAPIOperation> = Promise<InferredResponseType<TOp>>;
5
+ type RequestFunctionWithOptionalRequest<TOp extends OpenAPIOperation> = (request?: RequestConfig<TOp>) => ResponsePromise<TOp>;
6
+ type RequestFunctionWithRequiredRequest<TOp extends OpenAPIOperation> = (request: RequestConfig<TOp>) => ResponsePromise<TOp>;
7
+ export type RequestFunction<TOp extends OpenAPIOperation> = null extends RequestConfig<TOp> ? RequestFunctionWithOptionalRequest<TOp> : RequestFunctionWithRequiredRequest<TOp>;
8
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ import { HttpMediaType, HttpPayload, HttpStatus } from "./http.js";
2
+ import { AxiosResponse } from "axios";
3
+ export type Response<TContent extends HttpPayload = HttpPayload, TStatus extends HttpStatus = HttpStatus, TMediaType extends HttpMediaType | null = HttpMediaType> = AxiosResponse<TContent> & {
4
+ status: TStatus;
5
+ mediaType: TMediaType;
6
+ };
7
+ export type AnyResponse = Response<any, any, any>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,12 @@
1
+ export type HttpPayload = unknown;
2
+ export type HttpStatus = number | "default";
3
+ export type HttpMediaType = string;
4
+ type SafeHttpMethod = "GET" | "HEAD" | "OPTIONS";
5
+ type UnsafeHttpMethod = "PUT" | "DELETE" | "POST" | "PATCH";
6
+ export type HttpMethod = SafeHttpMethod | UnsafeHttpMethod;
7
+ type HeaderValue = string | number | boolean;
8
+ export type HttpHeaders = Partial<{
9
+ [TKey: string]: HeaderValue | HeaderValue[];
10
+ }>;
11
+ export type PathParameters = Record<string, string | number>;
12
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@mittwald/api-client-commons",
3
+ "description": "Common types and utilities for mittwald API clients",
4
+ "version": "1.0.3",
5
+ "license": "MIT",
6
+ "repository": "https://github.com/mittwald/api-client-js.git",
7
+ "homepage": "https://developer.mittwald.de",
8
+ "bugs": "https://github.com/mittwald/api-client-js/issues",
9
+ "type": "module",
10
+ "author": {
11
+ "name": "Mittwald CM Service GmbH & Co. KG",
12
+ "email": "opensource@mittwald.de"
13
+ },
14
+ "keywords": [
15
+ "mittwald",
16
+ "api",
17
+ "client",
18
+ "sdk",
19
+ "rest"
20
+ ],
21
+ "scripts": {
22
+ "test": "yarn test:tsd",
23
+ "test:tsd": "pnpify run tsd"
24
+ },
25
+ "files": [
26
+ "dist/**/*.{js,d.ts}"
27
+ ],
28
+ "dependencies": {
29
+ "@types/parse-path": "^7.0.0",
30
+ "axios": "^1.4.0",
31
+ "parse-path": "^7.0.0",
32
+ "path-to-regexp": "^6.2.1",
33
+ "type-fest": "^3.10.0"
34
+ },
35
+ "devDependencies": {
36
+ "@yarnpkg/pnpify": "^4.0.0-rc.43",
37
+ "tsd": "^0.28.1"
38
+ },
39
+ "types": "./dist/index.d.ts"
40
+ }