Skip to content

Commit 7adc5d6

Browse files
committed
fix: first release
1 parent a31e3c8 commit 7adc5d6

13 files changed

+76
-66
lines changed

.changeset/pre.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"initialVersions": {
55
"aspi": "0.0.1"
66
},
7-
"changesets": ["twenty-wasps-grow"]
7+
"changesets": [
8+
"twenty-wasps-grow"
9+
]
810
}

.changeset/twenty-wasps-grow.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"aspi": minor
2+
'aspi': minor
33
---
44

55
first release

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.changeset

.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"trailingComma": "all",
5+
"printWidth": 80,
6+
"tabWidth": 2
7+
}

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ I made this project because I am not happy with any of the Rest API clients avai
1616
## Example
1717

1818
```typescript
19-
import { aspi, Result } from "aspi";
19+
import { aspi, Result } from 'aspi';
2020

2121
const apiClient = new Aspi({
22-
baseUrl: "https://api.example.com",
22+
baseUrl: 'https://api.example.com',
2323
headers: {
24-
"Content-Type": "application/json",
24+
'Content-Type': 'application/json',
2525
},
2626
});
2727

2828
const getTodos = async (id: number) => {
2929
const response = await apiClient
3030
.get(`/todos/${id}`)
3131
.notFound(() => ({
32-
message: "Todo not found",
32+
message: 'Todo not found',
3333
}))
3434
.json<{
3535
id: number;
@@ -42,9 +42,9 @@ const getTodos = async (id: number) => {
4242
console.log(data);
4343
},
4444
onErr: (error) => {
45-
if (error.tag === "ASPI_ERROR") {
45+
if (error.tag === 'ASPI_ERROR') {
4646
console.error(error.response.status);
47-
} else if (error.tag === "NOT_FOUND") {
47+
} else if (error.tag === 'NOT_FOUND') {
4848
console.log(error.data.message);
4949
}
5050
},

src/aspi.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { HttpMethods } from "./http";
2-
import { Request } from "./request";
3-
import type { AspiConfig, AspiRequestConfig, Middleware } from "./types";
1+
import type { HttpMethods } from './http';
2+
import { Request } from './request';
3+
import type { AspiConfig, AspiRequestConfig, Middleware } from './types';
44

55
/**
66
* A class for making API requests with a base URL and configurable options
@@ -66,7 +66,7 @@ export class Aspi<TRequest extends RequestInit = RequestInit> {
6666
* const response = await api.get('/users').json();
6767
*/
6868
get(path: string) {
69-
return this.#createRequest("GET", path);
69+
return this.#createRequest('GET', path);
7070
}
7171

7272
/**
@@ -79,7 +79,7 @@ export class Aspi<TRequest extends RequestInit = RequestInit> {
7979
* const response = await api.post('/users', { name: 'John' }).json();
8080
*/
8181
post(path: string, body?: BodyInit) {
82-
return this.#createRequest("POST", path, body);
82+
return this.#createRequest('POST', path, body);
8383
}
8484

8585
/**
@@ -92,7 +92,7 @@ export class Aspi<TRequest extends RequestInit = RequestInit> {
9292
* const response = await api.put('/users/1', { name: 'John' }).json();
9393
*/
9494
put(path: string, body?: BodyInit) {
95-
return this.#createRequest("PUT", path, body);
95+
return this.#createRequest('PUT', path, body);
9696
}
9797

9898
/**
@@ -105,7 +105,7 @@ export class Aspi<TRequest extends RequestInit = RequestInit> {
105105
* const response = await api.patch('/users/1', { name: 'John' }).json();
106106
*/
107107
patch(path: string, body?: BodyInit) {
108-
return this.#createRequest("PATCH", path, body);
108+
return this.#createRequest('PATCH', path, body);
109109
}
110110

111111
/**
@@ -117,7 +117,7 @@ export class Aspi<TRequest extends RequestInit = RequestInit> {
117117
* const response = await api.delete('/users/1').json();
118118
*/
119119
delete(path: string) {
120-
return this.#createRequest("DELETE", path);
120+
return this.#createRequest('DELETE', path);
121121
}
122122

123123
/**
@@ -129,7 +129,7 @@ export class Aspi<TRequest extends RequestInit = RequestInit> {
129129
* const response = await api.head('/users').json();
130130
*/
131131
head(path: string) {
132-
return this.#createRequest("HEAD", path);
132+
return this.#createRequest('HEAD', path);
133133
}
134134

135135
/**
@@ -141,7 +141,7 @@ export class Aspi<TRequest extends RequestInit = RequestInit> {
141141
* const response = await api.options('/users').json();
142142
*/
143143
options(path: string) {
144-
return this.#createRequest("OPTIONS", path);
144+
return this.#createRequest('OPTIONS', path);
145145
}
146146

147147
/**
@@ -186,7 +186,7 @@ export class Aspi<TRequest extends RequestInit = RequestInit> {
186186
* api.setBearer('myAuthToken123');
187187
*/
188188
setBearer(token: string) {
189-
return this.setHeader("Authorization", `Bearer ${token}`);
189+
return this.setHeader('Authorization', `Bearer ${token}`);
190190
}
191191

192192
/**

src/error.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { HttpErrorCodes, HttpErrorStatus } from "./http";
1+
import type { HttpErrorCodes, HttpErrorStatus } from './http';
22

33
/**
44
* Error response interface for HTTP requests
@@ -36,7 +36,7 @@ export interface ErrorRequest extends RequestInit {
3636
* @property {ErrorResponse} response - The error response details
3737
*/
3838
export class AspiError extends Error {
39-
tag = "ASPI_ERROR" as const;
39+
tag = 'ASPI_ERROR' as const;
4040
request: ErrorRequest;
4141
response: ErrorResponse;
4242

src/http.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ export const getHttpErrorStatus = (status: HttpErrorCodes) => {
7474
* Valid HTTP methods as defined in the HTTP/1.1 specification.
7575
*/
7676
export type HttpMethods =
77-
| "GET"
78-
| "POST"
79-
| "PUT"
80-
| "DELETE"
81-
| "HEAD"
82-
| "OPTIONS"
83-
| "PATCH"
84-
| "TRACE"
85-
| "CONNECT";
77+
| 'GET'
78+
| 'POST'
79+
| 'PUT'
80+
| 'DELETE'
81+
| 'HEAD'
82+
| 'OPTIONS'
83+
| 'PATCH'
84+
| 'TRACE'
85+
| 'CONNECT';

src/index.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export * from "./aspi";
2-
export * from "./error";
3-
export * from "./http";
4-
export * from "./types";
5-
export * from "./result";
6-
export * from "./request";
1+
export * from './aspi';
2+
export * from './error';
3+
export * from './http';
4+
export * from './types';
5+
export * from './result';
6+
export * from './request';

src/request.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import {
33
type CustomError,
44
type ErrorRequest,
55
type ErrorResponse,
6-
} from "./error";
6+
} from './error';
77
import {
88
getHttpErrorStatus,
99
httpErrors,
1010
type HttpErrorCodes,
1111
type HttpErrorStatus,
1212
type HttpMethods,
13-
} from "./http";
14-
import type { AspiConfig, CustomErrorCb, Middleware } from "./types";
15-
import * as Result from "./result";
13+
} from './http';
14+
import type { AspiConfig, CustomErrorCb, Middleware } from './types';
15+
import * as Result from './result';
1616

1717
/**
1818
* A class for building and executing HTTP requests with customizable options and error handling.
@@ -117,7 +117,7 @@ export class Request<
117117
* request.setBearer('my-auth-token');
118118
*/
119119
setBearer(token: string) {
120-
return this.setHeader("Authorization", `Bearer ${token}`);
120+
return this.setHeader('Authorization', `Bearer ${token}`);
121121
}
122122

123123
body<Body extends {}>(body: Body) {
@@ -149,8 +149,8 @@ export class Request<
149149
return this as Request<
150150
Method,
151151
TRequest,
152-
Omit<Opts, "notFound"> & {
153-
notFound: CustomError<"NOT_FOUND", A>;
152+
Omit<Opts, 'notFound'> & {
153+
notFound: CustomError<'NOT_FOUND', A>;
154154
}
155155
>;
156156
}
@@ -172,8 +172,8 @@ export class Request<
172172
return this as Request<
173173
Method,
174174
TRequest,
175-
Omit<Opts, "unauthorised"> & {
176-
unauthorised: CustomError<"UNAUTHORIZED", A>;
175+
Omit<Opts, 'unauthorised'> & {
176+
unauthorised: CustomError<'UNAUTHORIZED', A>;
177177
}
178178
>;
179179
}
@@ -195,8 +195,8 @@ export class Request<
195195
return this as Request<
196196
Method,
197197
TRequest,
198-
Omit<Opts, "forbidden"> & {
199-
forbidden: CustomError<"FORBIDDEN", A>;
198+
Omit<Opts, 'forbidden'> & {
199+
forbidden: CustomError<'FORBIDDEN', A>;
200200
}
201201
>;
202202
}
@@ -268,10 +268,10 @@ export class Request<
268268
Result.Result<
269269
T,
270270
| AspiError
271-
| (Opts extends { notFound: any } ? Opts["notFound"] : never)
272-
| (Opts extends { unauthorised: any } ? Opts["unauthorised"] : never)
273-
| (Opts extends { forbidden: any } ? Opts["forbidden"] : never)
274-
| (Opts extends { httpError: any } ? Opts["httpError"] : never)
271+
| (Opts extends { notFound: any } ? Opts['notFound'] : never)
272+
| (Opts extends { unauthorised: any } ? Opts['unauthorised'] : never)
273+
| (Opts extends { forbidden: any } ? Opts['forbidden'] : never)
274+
| (Opts extends { httpError: any } ? Opts['httpError'] : never)
275275
>
276276
> {
277277
try {
@@ -283,8 +283,8 @@ export class Request<
283283
const response = await fetch(
284284
[
285285
new URL(this.#path, this.#baseUrl).toString(),
286-
this.#queryParams ? `?${this.#queryParams.toString()}` : "",
287-
].join(""),
286+
this.#queryParams ? `?${this.#queryParams.toString()}` : '',
287+
].join(''),
288288
requestInit,
289289
);
290290

@@ -324,11 +324,11 @@ export class Request<
324324
} catch (error) {
325325
return Result.err(
326326
new AspiError(
327-
error instanceof Error ? error.message : "Something went wrong",
327+
error instanceof Error ? error.message : 'Something went wrong',
328328
this.#request(),
329329
{
330330
status: 500,
331-
statusText: "INTERNAL_SERVER_ERROR",
331+
statusText: 'INTERNAL_SERVER_ERROR',
332332
},
333333
),
334334
);

src/result.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export type Ok<T> = { __tag: "ok"; value: T };
2-
export type Err<E> = { __tag: "err"; error: E };
1+
export type Ok<T> = { __tag: 'ok'; value: T };
2+
export type Err<E> = { __tag: 'err'; error: E };
33

44
/**
55
* Represents either a successful value of type T or an error of type E
@@ -15,15 +15,15 @@ export type Result<T, E> = Ok<T> | Err<E>;
1515
* const result = ok(42);
1616
* // { __tag: "ok", value: 42 }
1717
*/
18-
export const ok = <T>(value: T): Ok<T> => ({ __tag: "ok", value });
18+
export const ok = <T>(value: T): Ok<T> => ({ __tag: 'ok', value });
1919

2020
/**
2121
* Creates a failed Result
2222
* @example
2323
* const result = err("not found");
2424
* // { __tag: "err", error: "not found" }
2525
*/
26-
export const err = <E>(error: E): Err<E> => ({ __tag: "err", error });
26+
export const err = <E>(error: E): Err<E> => ({ __tag: 'err', error });
2727

2828
/**
2929
* Returns true if the result is a success
@@ -35,7 +35,7 @@ export const err = <E>(error: E): Err<E> => ({ __tag: "err", error });
3535
* isOk(error) // false
3636
*/
3737
export const isOk = <T, E>(result: Result<T, E>): result is Ok<T> =>
38-
result.__tag === "ok";
38+
result.__tag === 'ok';
3939

4040
/**
4141
* Returns true if the result is a failure

src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { ErrorRequest, ErrorResponse } from "./error";
1+
import type { ErrorRequest, ErrorResponse } from './error';
22

3-
export type AspiRequestConfig = Pick<RequestInit, "headers" | "mode">;
3+
export type AspiRequestConfig = Pick<RequestInit, 'headers' | 'mode'>;
44
export type CustomErrorCb<T, A extends {}> = (input: {
55
request: ErrorRequest;
66
response: ErrorResponse;

tsup.config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { defineConfig } from "tsup";
1+
import { defineConfig } from 'tsup';
22

33
export default defineConfig({
4-
entryPoints: ["src/index.ts"],
5-
format: ["cjs", "esm"],
4+
entryPoints: ['src/index.ts'],
5+
format: ['cjs', 'esm'],
66
dts: true,
7-
outDir: "dist",
7+
outDir: 'dist',
88
clean: true,
99
});

0 commit comments

Comments
 (0)