@unifetch/fortnox 1.0.1 → 2.0.0

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @unifetch/fortnox
2
2
 
3
- TypeScript SDK for the [Fortnox](https://www.fortnox.se/) API, fully typed from the official OpenAPI specification — with corrections applied where the spec has incorrect or overly permissive types.
3
+ TypeScript SDK for the [Fortnox API](https://api.fortnox.se/apidocs), fully typed from the official OpenAPI specification — with corrections applied where the spec has incorrect or overly permissive types.
4
4
 
5
5
  ## Installation
6
6
 
@@ -44,26 +44,31 @@ const fortnox = initFortnox({
44
44
 
45
45
  ## Usage
46
46
 
47
- Call `fortnox` with an API path (with autocomplete suggestions) to get a set of typed HTTP methods for that endpoint.
47
+ Every response is a discriminated union of `{ error: null; data: T }` or `{ error: ErrorResponse; data: null }`. Once you handle (or narrow) the error branch, TypeScript automatically infers that `data` is non-null.
48
+
49
+ There are two ways to call the API.
50
+
51
+ ### Resource-based API (recommended)
52
+
53
+ Access resources by name and call operations by their ID. Both the resource name and operation ID are autocompleted by TypeScript.
54
+
55
+ > **Note:** The operation names (e.g. `getList`, `create`, `bookkeep`) are **manually curated** and are not derived from the official Fortnox OpenAPI specification. The official spec uses operation ids that are inconsistent and ambiguous across endpoints. The names used here follow a consistent, human-readable convention defined in [`overrides/operation-ids.json`](overrides/operation-ids.json).
48
56
 
49
57
  ```ts
50
- const { data, error } = await fortnox("/3/invoices").get();
58
+ const { data, error } = await fortnox.invoices.getList();
51
59
 
52
60
  if (error) {
53
61
  console.error(error.ErrorInformation.message);
54
- // or handle the error some other way
55
62
  } else {
56
63
  // data is non-null here — TypeScript knows this after the error check
57
64
  console.log(data.Invoices);
58
65
  }
59
66
  ```
60
67
 
61
- Every response is a discriminated union of `{ error: null; data: T }` or `{ error: ErrorResponse; data: null }`. Once you handle (or narrow) the error branch, TypeScript automatically infers that `data` is non-null.
62
-
63
- ### With query parameters
68
+ #### With query parameters
64
69
 
65
70
  ```ts
66
- const { data, error } = await fortnox("/3/invoices").get({
71
+ const { data, error } = await fortnox.invoices.getList({
67
72
  query: { fromdate: "2024-01-01", todate: "2024-12-31" },
68
73
  });
69
74
 
@@ -74,10 +79,10 @@ for (const invoice of data.Invoices) {
74
79
  }
75
80
  ```
76
81
 
77
- ### Creating a resource
82
+ #### Creating a resource
78
83
 
79
84
  ```ts
80
- const { data, error } = await fortnox("/3/invoices").post({
85
+ const { data, error } = await fortnox.invoices.create({
81
86
  body: {
82
87
  Invoice: {
83
88
  CustomerNumber: "1",
@@ -93,10 +98,10 @@ if (error) throw error;
93
98
  console.log(data.Invoice.DocumentNumber);
94
99
  ```
95
100
 
96
- ### Fetching a single resource by ID
101
+ #### Fetching a single resource by ID
97
102
 
98
103
  ```ts
99
- const { data, error } = await fortnox("/3/invoices/{DocumentNumber}").get({
104
+ const { data, error } = await fortnox.invoices.get({
100
105
  params: { DocumentNumber: "100" },
101
106
  });
102
107
 
@@ -105,6 +110,32 @@ if (error) throw error;
105
110
  console.log(data.Invoice.CustomerName);
106
111
  ```
107
112
 
113
+ #### Calling an action on a resource
114
+
115
+ ```ts
116
+ const { data, error } = await fortnox.invoices.bookkeep({
117
+ params: { DocumentNumber: "100" },
118
+ });
119
+ ```
120
+
121
+ ### Path-based API
122
+
123
+ If you want to access an endpoint directly by its raw path, use `fortnox.path()`. This is equivalent to the resource-based API under the hood. The path and all the parameters are fully typed.
124
+
125
+ ```ts
126
+ const { data, error } = await fortnox.path("/3/invoices").get();
127
+
128
+ if (error) throw error;
129
+
130
+ console.log(data.Invoices);
131
+ ```
132
+
133
+ ```ts
134
+ const { data, error } = await fortnox.path("/3/invoices/{DocumentNumber}").get({
135
+ params: { DocumentNumber: "100" },
136
+ });
137
+ ```
138
+
108
139
  ## Type definitions
109
140
 
110
141
  ### `@unifetch/fortnox` (default) — patched types