Skip to content

Commit 2f93b4e

Browse files
committed
Prepare for publishing to JSR
1 parent fb70481 commit 2f93b4e

File tree

9 files changed

+486
-165
lines changed

9 files changed

+486
-165
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
files: cov.lcov
4242
- name: Release info
4343
if: |
44-
github.repository == 'udibo/http_error' &&
44+
github.repository == 'udibo/http-error' &&
4545
matrix.os == 'ubuntu-latest' &&
4646
matrix.deno == 'v1.x' &&
4747
startsWith(github.ref, 'refs/tags/')
@@ -52,7 +52,7 @@ jobs:
5252
if: env.RELEASE_VERSION != ''
5353
run: |
5454
mkdir -p target/release
55-
deno bundle mod.ts target/release/http_error_${RELEASE_VERSION}.js
55+
deno bundle mod.ts target/release/http-error.${RELEASE_VERSION}.js
5656
- name: Release
5757
uses: softprops/action-gh-release@v1
5858
if: env.RELEASE_VERSION != ''
@@ -61,4 +61,4 @@ jobs:
6161
with:
6262
draft: true
6363
files: |
64-
target/release/http_error_${{ env.RELEASE_VERSION }}.js
64+
target/release/http-error.${{ env.RELEASE_VERSION }}.js

.vscode/settings.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
"deno.enable": true,
33
"deno.lint": true,
44
"deno.unstable": false,
5-
"deno.suggest.imports.hosts": {
6-
"https://deno.land": true
5+
"deno.config": "./deno.jsonc",
6+
"files.associations": {
7+
"*.css": "tailwindcss"
8+
},
9+
"editor.formatOnSave": true,
10+
"editor.defaultFormatter": "denoland.vscode-deno",
11+
"editor.quickSuggestions": {
12+
"strings": true
713
}
814
}

README.md

Lines changed: 47 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,20 @@
11
# Http Error
22

3-
[![version](https://img.shields.io/badge/release-0.7.0-success)](https://deno.land/x/[email protected])
4-
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/[email protected]/mod.ts)
5-
[![CI](https://github.com/udibo/http_error/workflows/CI/badge.svg)](https://github.com/udibo/http_error/actions?query=workflow%3ACI)
6-
[![codecov](https://codecov.io/gh/udibo/http_error/branch/main/graph/badge.svg?token=8Q7TSUFWUY)](https://codecov.io/gh/udibo/http_error)
7-
[![license](https://img.shields.io/github/license/udibo/http_error)](https://github.com/udibo/http_error/blob/master/LICENSE)
3+
[![JSR](https://jsr.io/badges/@udibo/http-error)](https://jsr.io/@udibo/http-error)
4+
[![JSR Score](https://jsr.io/badges/@udibo/http-error/score)](https://jsr.io/@udibo/http-error)
5+
[![CI](https://github.com/udibo/http-error/workflows/CI/badge.svg)](https://github.com/udibo/http-error/actions?query=workflow%3ACI)
6+
[![codecov](https://codecov.io/gh/udibo/http-error/branch/main/graph/badge.svg?token=8Q7TSUFWUY)](https://codecov.io/gh/udibo/http-error)
7+
[![license](https://img.shields.io/github/license/udibo/http-error)](https://github.com/udibo/http-error/blob/master/LICENSE)
88

9-
An error class for HTTP requests.
9+
Utilities for creating and working with Http Errors.
1010

11-
This module was inspired by
11+
This package was inspired by
1212
[http-errors](https://www.npmjs.com/package/http-errors) for Node.js.
1313

1414
## Features
1515

1616
- Framework agnostic
1717

18-
## Installation
19-
20-
This is an ES Module written in TypeScript and can be used in Deno projects. ES
21-
Modules are the official standard format to package JavaScript code for reuse. A
22-
JavaScript bundle is provided with each release so that it can be used in
23-
Node.js packages or web browsers.
24-
25-
### Deno
26-
27-
To include it in a Deno project, you can import directly from the TS files. This
28-
module is available in Deno's third part module registry but can also be
29-
imported directly from GitHub using raw content URLs.
30-
31-
```ts
32-
// Import from Deno's third party module registry
33-
import { HttpError, isHttpError } from "https://deno.land/x/[email protected]/mod.ts";
34-
// Import from GitHub
35-
import { HttpError, isHttpError } "https://raw.githubusercontent.com/udibo/http_error/0.7.0/mod.ts";
36-
```
37-
38-
### Node.js
39-
40-
Node.js fully supports ES Modules.
41-
42-
If a Node.js package has the type "module" specified in its package.json file,
43-
the JavaScript bundle can be imported as a `.js` file.
44-
45-
```js
46-
import { HttpError, isHttpError } from "./http_error_0.7.0.js";
47-
```
48-
49-
The default type for Node.js packages is "commonjs". To import the bundle into a
50-
commonjs package, the file extension of the JavaScript bundle must be changed
51-
from `.js` to `.mjs`.
52-
53-
```js
54-
import { HttpError, isHttpError } from "./http_error_0.7.0.mjs";
55-
```
56-
57-
See [Node.js Documentation](https://nodejs.org/api/esm.html) for more
58-
information.
59-
60-
### Browser
61-
62-
Most modern browsers support ES Modules.
63-
64-
The JavaScript bundle can be imported into ES modules. Script tags for ES
65-
modules must have the type attribute set to "module".
66-
67-
```html
68-
<script type="module" src="main.js"></script>
69-
```
70-
71-
```js
72-
// main.js
73-
import { HttpError, isHttpError } from "./http_error_0.7.0.js";
74-
```
75-
76-
You can also embed a module script directly into an HTML file by placing the
77-
JavaScript code within the body of the script tag.
78-
79-
```html
80-
<script type="module">
81-
import { HttpError, isHttpError } from "./http_error_0.7.0.js";
82-
</script>
83-
```
84-
85-
See
86-
[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
87-
for more information.
88-
8918
## Usage
9019

9120
Below are some examples of how to use this module.
@@ -97,6 +26,8 @@ different call signatures you can use. The 4 examples below would throw the same
9726
error.
9827

9928
```ts
29+
import { HttpError } from "@udibo/http-error";
30+
10031
throw new HttpError(404, "file not found");
10132
throw new HttpError(404, { message: "file not found" });
10233
throw new HttpError("file not found", { status: 404 });
@@ -107,6 +38,8 @@ You can also include a cause in the optional options argument for it like you
10738
can with regular errors.
10839

10940
```ts
41+
import { HttpError } from "@udibo/http-error";
42+
11043
throw new HttpError(404, "file not found", { cause: error });
11144
```
11245

@@ -121,6 +54,8 @@ the name is not known for an HTTP error status code, it will default to
12154
UnknownClientError or UnknownServerError.
12255

12356
```ts
57+
import { HttpError } from "@udibo/http-error";
58+
12459
const error = new HttpError(404, "file not found");
12560
console.log(error.toString()); // NotFoundError: file not found
12661
```
@@ -129,6 +64,8 @@ If you would like to extend the HttpError class, you can pass your own error
12964
name in the options.
13065

13166
```ts
67+
import { HttpError, type HttpErrorOptions } from "@udibo/http-error";
68+
13269
class CustomError extends HttpError {
13370
constructor(
13471
message?: string,
@@ -144,6 +81,12 @@ signature, you can make use of the optionsFromArgs function. It will prioritize
14481
the status / message arguments over status / message options.
14582

14683
```ts
84+
import {
85+
HttpError,
86+
type HttpErrorOptions,
87+
optionsFromArgs,
88+
} from "@udibo/http-error";
89+
14790
class CustomError extends HttpError {
14891
constructor(
14992
status?: number,
@@ -175,6 +118,8 @@ will also return true for Error objects that have status and expose properties
175118
with matching types.
176119

177120
```ts
121+
import { HttpError, isHttpError } from "@udibo/http-error";
122+
178123
let error = new Error("file not found");
179124
console.log(isHttpError(error)); // false
180125
error = new HttpError(404, "file not found");
@@ -183,22 +128,35 @@ console.log(isHttpError(error)); // true
183128

184129
### ErrorResponse
185130

186-
This object can be used to transform an HttpError into a JSON format that can be
131+
This class can be used to transform an HttpError into a JSON format that can be
187132
converted back into an HttpError. This makes it easy for the server to share
188133
HttpError's with the client. This will work with any value that is thrown.
189134

190-
Here is an example of how an oak server could have middleware that convert an
135+
Here is an example of how an oak server could have middleware that converts an
191136
error into into a JSON format.
192137

193138
```ts
194-
app.use(async (ctx, next) => {
139+
import { Application } from "@oak/oak/application";
140+
import { ErrorResponse, HttpError } from "@udibo/http-error";
141+
142+
const app = new Application();
143+
144+
app.use(async (context, next) => {
195145
try {
196146
await next();
197147
} catch (error) {
148+
const { response } = context;
198149
response.status = isHttpError(error) ? error.status : 500;
199150
response.body = new ErrorResponse(error);
200151
}
201152
});
153+
154+
app.use(() => {
155+
// Will throw a 500 on every request.
156+
throw new HttpError(500);
157+
});
158+
159+
await app.listen({ port: 80 });
202160
```
203161

204162
When `JSON.stringify` is used on the ErrorResponse object, the ErrorResponse
@@ -209,6 +167,8 @@ that example, the response to the request would have it's status match the error
209167
and the body be a JSON representation of the error.
210168

211169
```ts
170+
import { HttpError } from "@udibo/http-error";
171+
212172
throw new HttpError(400, "Invalid input");
213173
```
214174

@@ -224,11 +184,6 @@ Then the response would have a 400 status and it's body would look like this:
224184
}
225185
```
226186

227-
If the format of your error responses is different than this, you can look at
228-
the source code in [mod.ts](/mod.ts) to see how you could create your own
229-
ErrorResponse object that can be used to convert your error responses into
230-
HttpErrors.
231-
232187
#### ErrorResponse.toError
233188

234189
This function gives a client the ability to convert the error response JSON into
@@ -238,6 +193,8 @@ In the following example, if getMovies is called and API endpoint returned an
238193
ErrorResponse, it would be converted into an HttpError object and be thrown.
239194

240195
```ts
196+
import { ErrorResponse, HttpError, isErrorResponse } from "@udibo/http-error";
197+
241198
async function getMovies() {
242199
const response = await fetch("https://example.com/movies.json");
243200
const movies = await response.json();
@@ -266,6 +223,8 @@ The error that `getMovies` would throw would be equivalent to throwing the
266223
following HttpError.
267224

268225
```ts
226+
import { HttpError } from "@udibo/http-error";
227+
269228
new HttpError(400, "Invalid input");
270229
```
271230

@@ -281,6 +240,8 @@ thrown. But if it isn't in that format and doesn't have an error status, the
281240
response body will be returned as the assumed movies.
282241

283242
```ts
243+
import { HttpError, isErrorResponse } from "@udibo/http-error";
244+
284245
async function getMovies() {
285246
const response = await fetch("https://example.com/movies.json");
286247
const movies = await response.json();

deno.jsonc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@udibo/http-error",
3+
"version": "0.8.0",
4+
"exports": {
5+
".": "./mod.ts"
6+
},
7+
"publish": {
8+
"include": [
9+
"LICENSE",
10+
"README.md",
11+
"**/*.ts"
12+
],
13+
"exclude": ["**/*.test.ts"]
14+
},
15+
"imports": {
16+
"@std/assert": "jsr:@std/assert@1",
17+
"@std/http": "jsr:@std/http@0",
18+
"@std/testing": "jsr:@std/testing@0"
19+
},
20+
"tasks": {
21+
// Checks the formatting and runs the linter.
22+
"check": "deno lint && deno fmt --check",
23+
// Gets your branch up to date with master after a squash merge.
24+
"git-rebase": "git fetch origin main && git rebase --onto origin/main HEAD"
25+
}
26+
}

deno.lock

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

deps.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)