4.16.0 • Published 15 days ago

@mittwald/api-client v4.16.0

Weekly downloads
-
License
MIT
Repository
github
Last release
15 days ago

mittwald API JavaScript client

This package contains a (mostly auto-generated) JavaScript client for the mittwald API.

License

Copyright (c) 2023 Mittwald CM Service GmbH & Co. KG and contributors

This project (and all NPM packages) therein is licensed under the MIT License; see the LICENSE file for details.

Table of contents

Installing

You can install this package from the regular NPM registry:

yarn add @mittwald/api-client

Usage

Import the client:

import { MittwaldAPIV2Client } from "@mittwald/api-client";

To create a client instance you can use one of the following factory method for different types of authentication:

  1. MittwaldAPIClient.newUnauthenticated()
  2. MittwaldAPIClient.newWithToken(apiToken: string) (recommended)
  3. MittwaldAPIClient.newWithCredentials(email: string, password: string) (does actually perform a login in the background and thus returns a promise)

Have a look at our API introduction for more information on how to obtain an API token and how to get started with the API.

Setting request parameters

API requests may require these type of parameters:

  • path parameters
  • headers
  • query parameters
  • request body

Path parameters

Path parameters are variable parts of a URL path. They are typically used to point to a specific resource within a collection, such as a project identified by ID. A URL can have several path parameters, each denoted with curly braces { }.

/v2/projects/{projectId}

Path parameters are required and must be directly defined inside the request object.

// Setting the projectId path parameters
const project = await mittwaldApi.project.getProject({
  projectId: "p-xxxxx",
});

Headers, query parameters and request body

Depending on the operation, you must configure additional parameters, such as queryParameters (URL query parameters), headers (HTTP headers), and data (request body).

The operations and their parameters are documented in the API documentation.

When using TypeScript all parameter schemas are reflected by the request type, so you will get compile errors, when a request does not match the schema.

const response = await mittwaldApi.category.operation({
  // path parameters
  pathParameter1: "param1",
  pathParameter2: "param2",
  // parameters in header
  headers: {
    "x-header": "header",
  },
  // query parameters
  queryParameters: {
    queryParameter1: "queryParam1",
    queryParameter2: "queryParam2",
  },
  // JSON object in request body
  data: {
    data1: "data1",
    data2: "data2",
  },
});

Example

import { MittwaldAPIV2Client } from "@mittwald/api-client";

const mittwaldApi = MittwaldAPIClient.newWithToken("your-access-token");

const projects = await mittwaldApi.project.listProjects();

Usage in React

This package also provides a client aligned to be used in React components. It uses @mittwald/react-use-promise to encapsulate the asynchronous API functions into AsyncResources. More details about how to use AsyncResources see the package documentation.

Installation

To use the React client you have to install the additional @mittwald/react-use-promise package:

yarn add @mittwald/react-use-promise

Setup

To create a React client instance, you first need an instance of the regular (promise-based) client. Then you can use the .fromBaseClient(api) method to build the React client.

const api = MittwaldAPIV2Client.newUnauthenticated();
const apiReact = MittwaldAPIV2ClientReact.fromBaseClient(api);

Usage

The React client has an equivalent for every GET method of the regular client. The methods returning an AsyncResource that can be used to get the API responses.

If the response is not OK (status 200), an ApiClientError will be thrown. Errors can be handled by using error-boundaries. See the error handling section for more details.

Example

import { MittwaldAPIV2Client } from "@mittwald/api-client";
import { MittwaldAPIV2ClientReact } from "@mittwald/api-client/react";

const api = MittwaldAPIV2Client.newUnauthenticated();
const apiReact = MittwaldAPIV2ClientReact.fromBaseClient(api);

const ProjectsList = () => {
  // apiReact.project.listProjects() returns an AsyncResource that can be "used"
  const projects = apiReact.project.listProjects().use({
    autoRefresh: {
      seconds: 30,
    },
  });

  return (
    <ul>
      {projects.map((p) => (
        <li key={p.id}>{p.description}</li>
      ))}
    </ul>
  );
};

API documentation

The API documentation can be found at https://api.mittwald.de/v2/docs/. You can find the operation ID on the right side of each operation.

Accessing the underlying Axios instance

The client uses the popular Axios HTTP client under the hood. You may access the Axios instance with the clients axios property.

Adding custom HTTP headers

To add custom HTTP headers to all requests you can use Axios' defaults.headers property:

client.axios.defaults.headers["User-Agent"] = `your-tool/v1.2.3`;

Intercepting requests or responses

To intercept requests or responses you can use Axios' interceptors.

Usage with TypeScript

All response and request types can be imported from the MittwaldAPIV2 namespace.

Referencing request/response types

import { MittwaldAPIV2 } from "@mittwald/api-client";

type ProjectData = MittwaldAPIV2.Operations.ProjectGetProject.ResponseData;

// Reference "non-200" response type
type ProjectNotFoundData =
  MittwaldAPIV2.Operations.ProjectGetProject.ResponseData<404>;

type CreateProjectData =
  MittwaldAPIV2.Operations.ProjectCreateProject.RequestData;

Migrating from package version V2 to V3

This instruction considers migrating from package version V2 to V3, which has a breaking change in the call signature of request calls. The API itself has not changed and is still at version 2.

Path parameters are a primary component of the request and thus should not be "hidden" in the request config object. In V3 the API of the request configuration object has changed, and you have to set the path parameters directly in the root level of the request object.

// V2
mittwaldApi.project.getProject({
  pathParameters: {
    projectId: "p-xxxxx",
  },
});

// V3
mittwaldApi.project.getProject({
  projectId: "p-xxxxx",
});
4.16.0

15 days ago

4.15.0

2 months ago

4.14.1

2 months ago

4.14.0

2 months ago

4.13.0

2 months ago

4.13.1

2 months ago

4.11.0

2 months ago

4.10.0

3 months ago

4.9.0

3 months ago

4.8.1

3 months ago

4.8.0

3 months ago

4.8.2

3 months ago

4.7.0

3 months ago

4.6.0

3 months ago

3.1.58

3 months ago

3.1.57

3 months ago

3.1.59

3 months ago

4.2.1-alpha.0

3 months ago

4.2.1

3 months ago

4.5.1

3 months ago

3.1.56

3 months ago

3.1.55

3 months ago

3.1.54

3 months ago

3.1.53

3 months ago

3.1.52

3 months ago

3.1.51

3 months ago

3.1.50

3 months ago

3.1.49

4 months ago

3.1.47

4 months ago

3.1.46

4 months ago

3.1.48

4 months ago

3.1.45

4 months ago

3.1.44

4 months ago

3.1.43

4 months ago

3.1.42

4 months ago

3.1.41

4 months ago

3.1.40

4 months ago

3.1.39

4 months ago

3.1.36

4 months ago

3.1.38

4 months ago

3.1.37

4 months ago

3.1.35

4 months ago

3.1.34

4 months ago

3.1.33

4 months ago

3.1.32

5 months ago

3.1.31

5 months ago

3.1.30

5 months ago

3.1.29

5 months ago

3.1.28

5 months ago

3.1.27

5 months ago

3.1.26

5 months ago

3.1.25

5 months ago

3.1.24

5 months ago

3.1.23

5 months ago

3.1.22

5 months ago

3.1.12

6 months ago

1.0.2

11 months ago

3.1.11

6 months ago

1.0.1

11 months ago

3.1.14

6 months ago

3.1.13

6 months ago

3.1.16

6 months ago

3.1.15

6 months ago

3.1.18

6 months ago

3.1.17

6 months ago

1.0.6

10 months ago

1.0.5

11 months ago

3.1.10

6 months ago

1.0.4

11 months ago

1.0.3

11 months ago

2.0.3

10 months ago

2.0.2

10 months ago

2.0.5

10 months ago

2.0.4

10 months ago

2.0.7

10 months ago

2.0.6

10 months ago

2.0.9

10 months ago

2.0.8

10 months ago

3.1.9

6 months ago

3.1.8

6 months ago

2.0.1

10 months ago

3.0.12

8 months ago

3.0.4

8 months ago

3.0.13

8 months ago

3.0.3

8 months ago

3.0.10

8 months ago

3.0.2

8 months ago

3.0.11

8 months ago

3.0.1

9 months ago

3.0.16

7 months ago

3.0.8

8 months ago

3.0.17

7 months ago

3.0.7

8 months ago

3.0.14

8 months ago

3.0.6

8 months ago

3.0.15

8 months ago

3.0.5

8 months ago

3.1.21

6 months ago

3.1.20

6 months ago

1.0.0-alpha.20

11 months ago

3.1.19

6 months ago

3.0.23

7 months ago

3.0.24

7 months ago

3.0.21

7 months ago

3.0.22

7 months ago

3.0.27

7 months ago

3.0.28

7 months ago

3.0.25

7 months ago

3.0.26

7 months ago

3.0.20

7 months ago

2.0.15

9 months ago

2.0.16

9 months ago

2.0.13

9 months ago

2.0.14

9 months ago

2.0.11

10 months ago

2.0.12

9 months ago

2.0.10

10 months ago

3.0.18

7 months ago

3.0.19

7 months ago

3.0.9

8 months ago

3.1.3

6 months ago

3.1.2

6 months ago

3.1.1

6 months ago

3.1.7

6 months ago

3.1.6

6 months ago

3.1.5

6 months ago

3.1.4

6 months ago

2.0.19

9 months ago

2.0.17

9 months ago

2.0.18

9 months ago

2.0.22

9 months ago

2.0.20

9 months ago

2.0.21

9 months ago

3.0.29

7 months ago

1.0.0-alpha.19

11 months ago

1.0.0-alpha.18

11 months ago

1.0.0-alpha.17

11 months ago

1.0.0-alpha.16

11 months ago

1.0.0-alpha.15

11 months ago

1.0.0-alpha.14

11 months ago

1.0.0-alpha.13

11 months ago

1.0.0-alpha.12

12 months ago