Skip to content

Commit

Permalink
Prepare for publishing to JSR
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJune committed Jul 27, 2024
1 parent fb70481 commit 2f93b4e
Show file tree
Hide file tree
Showing 9 changed files with 486 additions and 165 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
files: cov.lcov
- name: Release info
if: |
github.repository == 'udibo/http_error' &&
github.repository == 'udibo/http-error' &&
matrix.os == 'ubuntu-latest' &&
matrix.deno == 'v1.x' &&
startsWith(github.ref, 'refs/tags/')
Expand All @@ -52,7 +52,7 @@ jobs:
if: env.RELEASE_VERSION != ''
run: |
mkdir -p target/release
deno bundle mod.ts target/release/http_error_${RELEASE_VERSION}.js
deno bundle mod.ts target/release/http-error.${RELEASE_VERSION}.js
- name: Release
uses: softprops/action-gh-release@v1
if: env.RELEASE_VERSION != ''
Expand All @@ -61,4 +61,4 @@ jobs:
with:
draft: true
files: |
target/release/http_error_${{ env.RELEASE_VERSION }}.js
target/release/http-error.${{ env.RELEASE_VERSION }}.js
10 changes: 8 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
"deno.enable": true,
"deno.lint": true,
"deno.unstable": false,
"deno.suggest.imports.hosts": {
"https://deno.land": true
"deno.config": "./deno.jsonc",
"files.associations": {
"*.css": "tailwindcss"
},
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.quickSuggestions": {
"strings": true
}
}
133 changes: 47 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,91 +1,20 @@
# Http Error

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

An error class for HTTP requests.
Utilities for creating and working with Http Errors.

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

## Features

- Framework agnostic

## Installation

This is an ES Module written in TypeScript and can be used in Deno projects. ES
Modules are the official standard format to package JavaScript code for reuse. A
JavaScript bundle is provided with each release so that it can be used in
Node.js packages or web browsers.

### Deno

To include it in a Deno project, you can import directly from the TS files. This
module is available in Deno's third part module registry but can also be
imported directly from GitHub using raw content URLs.

```ts
// Import from Deno's third party module registry
import { HttpError, isHttpError } from "https://deno.land/x/[email protected]/mod.ts";
// Import from GitHub
import { HttpError, isHttpError } "https://raw.githubusercontent.com/udibo/http_error/0.7.0/mod.ts";
```
### Node.js
Node.js fully supports ES Modules.
If a Node.js package has the type "module" specified in its package.json file,
the JavaScript bundle can be imported as a `.js` file.
```js
import { HttpError, isHttpError } from "./http_error_0.7.0.js";
```

The default type for Node.js packages is "commonjs". To import the bundle into a
commonjs package, the file extension of the JavaScript bundle must be changed
from `.js` to `.mjs`.

```js
import { HttpError, isHttpError } from "./http_error_0.7.0.mjs";
```

See [Node.js Documentation](https://nodejs.org/api/esm.html) for more
information.

### Browser

Most modern browsers support ES Modules.

The JavaScript bundle can be imported into ES modules. Script tags for ES
modules must have the type attribute set to "module".

```html
<script type="module" src="main.js"></script>
```

```js
// main.js
import { HttpError, isHttpError } from "./http_error_0.7.0.js";
```

You can also embed a module script directly into an HTML file by placing the
JavaScript code within the body of the script tag.

```html
<script type="module">
import { HttpError, isHttpError } from "./http_error_0.7.0.js";
</script>
```

See
[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules)
for more information.

## Usage

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

```ts
import { HttpError } from "@udibo/http-error";

throw new HttpError(404, "file not found");
throw new HttpError(404, { message: "file not found" });
throw new HttpError("file not found", { status: 404 });
Expand All @@ -107,6 +38,8 @@ You can also include a cause in the optional options argument for it like you
can with regular errors.

```ts
import { HttpError } from "@udibo/http-error";

throw new HttpError(404, "file not found", { cause: error });
```

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

```ts
import { HttpError } from "@udibo/http-error";

const error = new HttpError(404, "file not found");
console.log(error.toString()); // NotFoundError: file not found
```
Expand All @@ -129,6 +64,8 @@ If you would like to extend the HttpError class, you can pass your own error
name in the options.

```ts
import { HttpError, type HttpErrorOptions } from "@udibo/http-error";

class CustomError extends HttpError {
constructor(
message?: string,
Expand All @@ -144,6 +81,12 @@ signature, you can make use of the optionsFromArgs function. It will prioritize
the status / message arguments over status / message options.

```ts
import {
HttpError,
type HttpErrorOptions,
optionsFromArgs,
} from "@udibo/http-error";

class CustomError extends HttpError {
constructor(
status?: number,
Expand Down Expand Up @@ -175,6 +118,8 @@ will also return true for Error objects that have status and expose properties
with matching types.

```ts
import { HttpError, isHttpError } from "@udibo/http-error";

let error = new Error("file not found");
console.log(isHttpError(error)); // false
error = new HttpError(404, "file not found");
Expand All @@ -183,22 +128,35 @@ console.log(isHttpError(error)); // true

### ErrorResponse

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

Here is an example of how an oak server could have middleware that convert an
Here is an example of how an oak server could have middleware that converts an
error into into a JSON format.

```ts
app.use(async (ctx, next) => {
import { Application } from "@oak/oak/application";
import { ErrorResponse, HttpError } from "@udibo/http-error";

const app = new Application();

app.use(async (context, next) => {
try {
await next();
} catch (error) {
const { response } = context;
response.status = isHttpError(error) ? error.status : 500;
response.body = new ErrorResponse(error);
}
});

app.use(() => {
// Will throw a 500 on every request.
throw new HttpError(500);
});

await app.listen({ port: 80 });
```

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

```ts
import { HttpError } from "@udibo/http-error";

throw new HttpError(400, "Invalid input");
```

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

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

#### ErrorResponse.toError

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

```ts
import { ErrorResponse, HttpError, isErrorResponse } from "@udibo/http-error";

async function getMovies() {
const response = await fetch("https://example.com/movies.json");
const movies = await response.json();
Expand Down Expand Up @@ -266,6 +223,8 @@ The error that `getMovies` would throw would be equivalent to throwing the
following HttpError.

```ts
import { HttpError } from "@udibo/http-error";

new HttpError(400, "Invalid input");
```

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

```ts
import { HttpError, isErrorResponse } from "@udibo/http-error";

async function getMovies() {
const response = await fetch("https://example.com/movies.json");
const movies = await response.json();
Expand Down
26 changes: 26 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@udibo/http-error",
"version": "0.8.0",
"exports": {
".": "./mod.ts"
},
"publish": {
"include": [
"LICENSE",
"README.md",
"**/*.ts"
],
"exclude": ["**/*.test.ts"]
},
"imports": {
"@std/assert": "jsr:@std/assert@1",
"@std/http": "jsr:@std/http@0",
"@std/testing": "jsr:@std/testing@0"
},
"tasks": {
// Checks the formatting and runs the linter.
"check": "deno lint && deno fmt --check",
// Gets your branch up to date with master after a squash merge.
"git-rebase": "git fetch origin main && git rebase --onto origin/main HEAD"
}
}
43 changes: 43 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions deps.ts

This file was deleted.

Loading

0 comments on commit 2f93b4e

Please sign in to comment.