Making requests
Methods
// Server:
const routes = {
foo: builder.get(/**/),
bar: builder.post(/**/),
baz: builder.put(/**/),
qux: builder.patch(/**/),
quux: builder.delete(/**/),
};
// Client:
await fetchCuple(client.foo.get);
await fetchCuple(client.bar.post);
await fetchCuple(client.baz.put);
await fetchCuple(client.qux.patch);
await fetchCuple(client.quux.delete);
Why specify the method on the client-side?
Since Cuple doesn't have a code generator, you have to tell the HTTP method in order to build up the fetch request with the right method. It's type-checked so you cannot specify methods other than what the server specifies.
Parameters:
body: The request body
headers: HTTP headers
params: URL parameters (e.g.: in case of /post/:id, it can be { id: "1" })
query: query parameters (e.g.: in case of ?id=2, it can be { id: "2" })
signal: an AbortSignal
Every parameter's type is auto-infered from the server.
await fetchCuple(client.user.updateUser.patch, {
body: { name: 'David' },
headers: { authorization: 'Bearer ...' },
params: { id: 12 },
query: {
'foo': 'bar'
}
})
Return type
The server's route declares every result it can send, and the client knows them all:
// What client.getPosts.get can answer:
| { result: "success"; statusCode: 200; posts: Post[] }
| { result: "unexpected-error"; statusCode: 500; message: string }
fetchCuple resolves with the success one and rejects the others, unless you list them. See Handling results.