Skip to content

Commit

Permalink
Merge pull request #2 from udibo/dev
Browse files Browse the repository at this point in the history
Change ErrorInit to ErrorOptions for new version of TypeScript
  • Loading branch information
KyleJune authored Mar 30, 2022
2 parents f30ec4b + 979fb10 commit 7a0d9b7
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 54 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Http Error

[![version](https://img.shields.io/badge/release-0.1.2-success)](https://deno.land/x/http_error@0.1.2)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/http_error@0.1.2/mod.ts)
[![version](https://img.shields.io/badge/release-0.1.3-success)](https://deno.land/x/http_error@0.1.3)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/http_error@0.1.3/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)
Expand Down Expand Up @@ -30,9 +30,9 @@ 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/http_error@0.1.2/mod.ts";
import { HttpError, isHttpError } from "https://deno.land/x/http_error@0.1.3/mod.ts";
// Import from GitHub
import { HttpError, isHttpError } "https://raw.githubusercontent.com/udibo/http_error/0.1.2/mod.ts";
import { HttpError, isHttpError } "https://raw.githubusercontent.com/udibo/http_error/0.1.3/mod.ts";
```
### Node.js
Expand All @@ -43,15 +43,15 @@ 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.1.2.js";
import { HttpError, isHttpError } from "./http_error_0.1.3.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.1.2.mjs";
import { HttpError, isHttpError } from "./http_error_0.1.3.mjs";
```

See [Node.js Documentation](https://nodejs.org/api/esm.html) for more
Expand All @@ -70,15 +70,15 @@ modules must have the type attribute set to "module".

```js
// main.js
import { HttpError, isHttpError } from "./http_error_0.1.2.js";
import { HttpError, isHttpError } from "./http_error_0.1.3.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.1.2.js";
import { HttpError, isHttpError } from "./http_error_0.1.3.js";
</script>
```

Expand Down Expand Up @@ -132,7 +132,7 @@ name in the options.
class CustomError extends HttpError {
constructor(
message?: string,
options?: HttpErrorInit,
options?: HttpErrorOptions,
) {
super(message, { name: "CustomError", status: 420, ...options });
}
Expand All @@ -148,15 +148,15 @@ class CustomError extends HttpError {
constructor(
status?: number,
message?: string,
options?: HttpErrorInit,
options?: HttpErrorOptions,
);
constructor(status?: number, options?: HttpErrorInit);
constructor(message?: string, options?: HttpErrorInit);
constructor(options?: HttpErrorInit);
constructor(status?: number, options?: HttpErrorOptions);
constructor(message?: string, options?: HttpErrorOptions);
constructor(options?: HttpErrorOptions);
constructor(
statusOrMessageOrOptions?: number | string | HttpErrorInit,
messageOrOptions?: string | HttpErrorInit,
options?: HttpErrorInit,
statusOrMessageOrOptions?: number | string | HttpErrorOptions,
messageOrOptions?: string | HttpErrorOptions,
options?: HttpErrorOptions,
) {
const init = optionsFromArgs(
statusOrMessageOrOptions,
Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export {
Status,
STATUS_TEXT,
} from "https://deno.land/std@0.111.0/http/http_status.ts";
} from "https://deno.land/std@0.132.0/http/http_status.ts";
20 changes: 11 additions & 9 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Status, STATUS_TEXT } from "./deps.ts";

/** Options for initializing an HttpError. */
export interface HttpErrorInit extends ErrorInit {
export interface HttpErrorOptions extends ErrorOptions {
/** The name of the error. Default based on error status. */
name?: string;
message?: string;
Expand All @@ -18,7 +18,9 @@ export interface HttpErrorInit extends ErrorInit {
* Converts HttpError arguments to an options object.
* Prioritizing status and message arguments over status and message options.
*/
export function optionsFromArgs<Init extends HttpErrorInit = HttpErrorInit>(
export function optionsFromArgs<
Init extends HttpErrorOptions = HttpErrorOptions,
>(
statusOrMessageOrOptions?: number | string | Init,
messageOrOptions?: string | Init,
options?: Init,
Expand Down Expand Up @@ -79,15 +81,15 @@ export class HttpError extends Error {
constructor(
status?: number,
message?: string,
options?: HttpErrorInit,
options?: HttpErrorOptions,
);
constructor(status?: number, options?: HttpErrorInit);
constructor(message?: string, options?: HttpErrorInit);
constructor(options?: HttpErrorInit);
constructor(status?: number, options?: HttpErrorOptions);
constructor(message?: string, options?: HttpErrorOptions);
constructor(options?: HttpErrorOptions);
constructor(
statusOrMessageOrOptions?: number | string | HttpErrorInit,
messageOrOptions?: string | HttpErrorInit,
options?: HttpErrorInit,
statusOrMessageOrOptions?: number | string | HttpErrorOptions,
messageOrOptions?: string | HttpErrorOptions,
options?: HttpErrorOptions,
) {
const init = optionsFromArgs(
statusOrMessageOrOptions,
Expand Down
56 changes: 30 additions & 26 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Status } from "./deps.ts";
import {
HttpError,
HttpErrorInit,
HttpErrorOptions,
isHttpError,
optionsFromArgs,
} from "./mod.ts";
import { assertEquals, assertThrows, test, TestSuite } from "./test_deps.ts";
import { assertEquals, assertThrows, describe, it } from "./test_deps.ts";

const httpErrorTests = new TestSuite({ name: "HttpError" });
const httpErrorTests = describe("HttpError");

test(httpErrorTests, "without args", () => {
it(httpErrorTests, "without args", () => {
const error = new HttpError();
assertEquals(error.toString(), "InternalServerError");
assertEquals(error.name, "InternalServerError");
Expand All @@ -19,7 +19,7 @@ test(httpErrorTests, "without args", () => {
assertEquals(error.cause, undefined);
});

test(httpErrorTests, "with status", () => {
it(httpErrorTests, "with status", () => {
function assertWithStatus(error: HttpError): void {
assertEquals(error.status, 400);
assertEquals(error.expose, true);
Expand All @@ -29,7 +29,7 @@ test(httpErrorTests, "with status", () => {
assertWithStatus(new HttpError(undefined, { status: 400 }));
});

test(httpErrorTests, "with message", () => {
it(httpErrorTests, "with message", () => {
function assertWithMessage(error: HttpError): void {
assertEquals(error.toString(), "InternalServerError: something went wrong");
assertEquals(error.message, "something went wrong");
Expand All @@ -42,7 +42,7 @@ test(httpErrorTests, "with message", () => {
);
});

test(
it(
httpErrorTests,
"prefer status/message args over status/messagee options",
() => {
Expand Down Expand Up @@ -82,7 +82,7 @@ test(
},
);

test(httpErrorTests, "with cause", () => {
it(httpErrorTests, "with cause", () => {
const cause = new Error("fail");
function assertWithCause(error: HttpError): void {
assertEquals(error.cause, cause);
Expand All @@ -92,7 +92,7 @@ test(httpErrorTests, "with cause", () => {
assertWithCause(new HttpError(undefined, undefined, { cause }));
});

test(httpErrorTests, "invalid status", () => {
it(httpErrorTests, "invalid status", () => {
assertThrows(
() => new HttpError(-500),
RangeError,
Expand Down Expand Up @@ -183,14 +183,14 @@ function assertName(
);
}

test(httpErrorTests, "default name", () => {
it(httpErrorTests, "default name", () => {
const message = "something went wrong";
for (let status = 400; status < 600; status++) {
assertName(new HttpError(status, message), status, message);
}
});

test(httpErrorTests, "override name", () => {
it(httpErrorTests, "override name", () => {
const message = "something went wrong";
for (let status = 400; status < 600; status++) {
assertName(
Expand Down Expand Up @@ -219,20 +219,20 @@ function assertExpose(
);
}

test(httpErrorTests, "default expose", () => {
it(httpErrorTests, "default expose", () => {
for (let status = 400; status < 600; status++) {
assertExpose(new HttpError(status), status);
}
});

test(httpErrorTests, "override expose", () => {
it(httpErrorTests, "override expose", () => {
for (let status = 400; status < 600; status++) {
const expose = status >= 500;
assertExpose(new HttpError(status, { expose }), status, expose);
}
});

test(httpErrorTests, "with all options", () => {
it(httpErrorTests, "with all options", () => {
const cause = new Error("fail");
function assertAllOptions(error: HttpError) {
assertEquals(error.toString(), "CustomError: something went wrong");
Expand Down Expand Up @@ -276,7 +276,7 @@ test(httpErrorTests, "with all options", () => {
);
});

test("isHttpError", () => {
it("isHttpError", () => {
assertEquals(isHttpError(new Error()), false);
assertEquals(isHttpError(new HttpError()), true);
assertEquals(isHttpError(new HttpError(400, "something went wrong")), true);
Expand Down Expand Up @@ -305,9 +305,9 @@ test("isHttpError", () => {
assertEquals(isHttpError(new OtherHttpError(400, "failed")), true);
});

const optionsFromArgsTests = new TestSuite({ name: "optionsFromArgs" });
const optionsFromArgsTests = describe("optionsFromArgs");

test(
it(
optionsFromArgsTests,
"prefer status/message args over status/messagee options",
() => {
Expand All @@ -316,7 +316,7 @@ test(
const options = { message: messages[1], status: statuses[1] };

function assertPreferArgs(
options: HttpErrorInit,
options: HttpErrorOptions,
expectedStatus: number,
expectedMessage: string,
): void {
Expand Down Expand Up @@ -344,33 +344,37 @@ test(
},
);

test(
it(
optionsFromArgsTests,
"supports extended options",
() => {
const status = 400;
const message = "something went wrong";
const options = { code: "invalid_request", uri: "https://example.com" };
interface ExtendedErrorInit extends HttpErrorInit {
interface ExtendedErrorOptions extends HttpErrorOptions {
code?: string;
uri?: string;
}
const expectedOptions: ExtendedErrorInit = { status, message, ...options };
function assertExtendedInit(options: ExtendedErrorInit): void {
const expectedOptions: ExtendedErrorOptions = {
status,
message,
...options,
};
function assertExtendedInit(options: ExtendedErrorOptions): void {
assertEquals(options, expectedOptions);
}

assertExtendedInit(
optionsFromArgs<ExtendedErrorInit>(status, message, options),
optionsFromArgs<ExtendedErrorOptions>(status, message, options),
);
assertExtendedInit(
optionsFromArgs<ExtendedErrorInit>(status, { message, ...options }),
optionsFromArgs<ExtendedErrorOptions>(status, { message, ...options }),
);
assertExtendedInit(
optionsFromArgs<ExtendedErrorInit>(message, { status, ...options }),
optionsFromArgs<ExtendedErrorOptions>(message, { status, ...options }),
);
assertExtendedInit(
optionsFromArgs<ExtendedErrorInit>({ status, message, ...options }),
optionsFromArgs<ExtendedErrorOptions>({ status, message, ...options }),
);
},
);
4 changes: 2 additions & 2 deletions test_deps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export {
assertEquals,
assertThrows,
} from "https://deno.land/std@0.111.0/testing/asserts.ts";
export { test, TestSuite } from "https://deno.land/x/test_suite@0.9.0/mod.ts";
} from "https://deno.land/std@0.132.0/testing/asserts.ts";
export { describe, it } from "https://deno.land/x/test_suite@0.14.0/mod.ts";

0 comments on commit 7a0d9b7

Please sign in to comment.