@unifetch/fortnox 1.0.1 → 2.0.1

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,8 @@
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
+
5
+ Zero dependencies. Fully compatible with edge runtimes such as [Cloudflare Workers](https://workers.cloudflare.com/) and Vercel Edge Functions.
4
6
 
5
7
  ## Installation
6
8
 
@@ -44,26 +46,31 @@ const fortnox = initFortnox({
44
46
 
45
47
  ## Usage
46
48
 
47
- Call `fortnox` with an API path (with autocomplete suggestions) to get a set of typed HTTP methods for that endpoint.
49
+ 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.
50
+
51
+ There are two ways to call the API.
52
+
53
+ ### Resource-based API (recommended)
54
+
55
+ Access resources by name and call operations by their ID. Both the resource name and operation ID are autocompleted by TypeScript.
56
+
57
+ > **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
58
 
49
59
  ```ts
50
- const { data, error } = await fortnox("/3/invoices").get();
60
+ const { data, error } = await fortnox.invoices.getList();
51
61
 
52
62
  if (error) {
53
63
  console.error(error.ErrorInformation.message);
54
- // or handle the error some other way
55
64
  } else {
56
65
  // data is non-null here — TypeScript knows this after the error check
57
66
  console.log(data.Invoices);
58
67
  }
59
68
  ```
60
69
 
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
70
+ #### With query parameters
64
71
 
65
72
  ```ts
66
- const { data, error } = await fortnox("/3/invoices").get({
73
+ const { data, error } = await fortnox.invoices.getList({
67
74
  query: { fromdate: "2024-01-01", todate: "2024-12-31" },
68
75
  });
69
76
 
@@ -74,10 +81,10 @@ for (const invoice of data.Invoices) {
74
81
  }
75
82
  ```
76
83
 
77
- ### Creating a resource
84
+ #### Creating a resource
78
85
 
79
86
  ```ts
80
- const { data, error } = await fortnox("/3/invoices").post({
87
+ const { data, error } = await fortnox.invoices.create({
81
88
  body: {
82
89
  Invoice: {
83
90
  CustomerNumber: "1",
@@ -93,10 +100,10 @@ if (error) throw error;
93
100
  console.log(data.Invoice.DocumentNumber);
94
101
  ```
95
102
 
96
- ### Fetching a single resource by ID
103
+ #### Fetching a single resource by ID
97
104
 
98
105
  ```ts
99
- const { data, error } = await fortnox("/3/invoices/{DocumentNumber}").get({
106
+ const { data, error } = await fortnox.invoices.get({
100
107
  params: { DocumentNumber: "100" },
101
108
  });
102
109
 
@@ -105,6 +112,32 @@ if (error) throw error;
105
112
  console.log(data.Invoice.CustomerName);
106
113
  ```
107
114
 
115
+ #### Calling an action on a resource
116
+
117
+ ```ts
118
+ const { data, error } = await fortnox.invoices.bookkeep({
119
+ params: { DocumentNumber: "100" },
120
+ });
121
+ ```
122
+
123
+ ### Path-based API
124
+
125
+ 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.
126
+
127
+ ```ts
128
+ const { data, error } = await fortnox.path("/3/invoices").get();
129
+
130
+ if (error) throw error;
131
+
132
+ console.log(data.Invoices);
133
+ ```
134
+
135
+ ```ts
136
+ const { data, error } = await fortnox.path("/3/invoices/{DocumentNumber}").get({
137
+ params: { DocumentNumber: "100" },
138
+ });
139
+ ```
140
+
108
141
  ## Type definitions
109
142
 
110
143
  ### `@unifetch/fortnox` (default) — patched types