Skip to content

Commit

Permalink
feat: implement in Deno (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fdionisi authored Dec 20, 2022
1 parent 79aca26 commit b82dba8
Show file tree
Hide file tree
Showing 17 changed files with 107 additions and 2,460 deletions.
5 changes: 0 additions & 5 deletions .commitlintrc.json

This file was deleted.

41 changes: 20 additions & 21 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish Package to npmjs
name: Publish Package to npm

on:
release:
Expand All @@ -11,30 +11,29 @@ jobs:
- name: Checkout git repository
uses: actions/checkout@v3

- name: Use Node.js 18.x
uses: actions/setup-node@v3
- name: Setup Deno - Latest
uses: denoland/setup-deno@v1
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
deno-version: vx.x.x

- name: Cache dependencies
uses: actions/cache@v3
- name: Setup Node.js - 18.x
uses: actions/setup-node@v3
with:
path: |
node_modules
*/*/node_modules
key: yarn-deps-${{ hashFiles('**/yarn.lock') }}
node-version: "18.x"
registry-url: "https://registry.npmjs.org"

- name: Install dependencies
run: |
yarn
- name: Get tag version
if: startsWith(github.ref, 'refs/tags/')
id: get_tag_version
run: echo ::set-output name=TAG_VERSION::${GITHUB_REF/refs\/tags\//}

- name: Run tests
run: yarn test
- name: Build for npm
run: deno run -A ./scripts/build.ts ${{steps.get_tag_version.outputs.TAG_VERSION}}

- name: Transpile to JavaScript
run: yarn transpile

- run: npm publish --access=public
- name: Publish to npm
if: startsWith(github.ref, 'refs/tags/')
run: |
cd npm
npm publish --access=public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
55 changes: 7 additions & 48 deletions .github/workflows/pull-request-testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ name: Pull request testing

on:
push:
branches-ignore:
branches-ignore:
- main
paths:
- '.github/workflows/pull-request-testing.yml'
- 'jest.config.ts'
- 'src/**/*.ts'
- ".github/workflows/pull-request-testing.yml"
- "**/*.ts"

jobs:
dependencies:
Expand All @@ -17,52 +16,12 @@ jobs:
- name: Check out Git repository
uses: actions/checkout@v3

- name: Use Node.js 18.x
uses: actions/setup-node@v1
- name: Setup Deno - Latest
uses: denoland/setup-deno@v1
with:
node-version: '18.x'

- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
node_modules
*/*/node_modules
key: yarn-deps-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
run: |
yarn
unit:
name: Unit and integration tests
needs: dependencies
runs-on: ubuntu-latest
steps:
- name: Check out Git repository
uses: actions/checkout@v3

- name: Wait for Docker environment to be ready
run: ./scripts/wait-catena-x-at-home-ready.sh

- name: Use Node.js 18.x
uses: actions/setup-node@v1
with:
node-version: '18.x'

- name: Cache dependencies
uses: actions/cache@v3
with:
path: |
node_modules
*/*/node_modules
key: yarn-deps-${{ hashFiles('**/yarn.lock') }}

- name: Install dependencies
run: |
yarn
deno-version: vx.x.x

- name: Run tests
run: yarn test
run: deno test
env:
CI: true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage/
dist/
node_modules/
node_modules/
npm/
8 changes: 0 additions & 8 deletions .lintstagedrc.json

This file was deleted.

6 changes: 0 additions & 6 deletions .npmignore

This file was deleted.

8 changes: 0 additions & 8 deletions .prettierrc.json

This file was deleted.

3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deno.enable": true
}
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1>TypedError ⌨️💥</h1>
<p>
<b>
JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" target="_blank">Error</a> for TypeScript projects.
A better JavaScript <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" target="_blank">Error</a> for Node.js, Deno and the browser.
</b>
</p>
<sub>
Expand All @@ -17,7 +17,7 @@ large projects.
The [JavaScript `Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
is a necessary component of the error handling story, but it's often too limited.

The main difficulty is categorising errors; when building the main business logic
The main difficulty is categorizing errors; when building the main business logic
of any project, it's common to encounter similar errors (e.g. duplicate entities,
forbidden access, etc.), and relying on
[`Error.prototype.message`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/message)
Expand All @@ -29,6 +29,8 @@ a smoother error handling story.

## Usage

### Node.js

Install via `npm` or `yarn`

```sh
Expand All @@ -39,11 +41,17 @@ npm install @think-it-labs/typed-error
yarn add @think-it-labs/typed-error
```

Then it's possible to create a custom `Error` class extending the `TypedError`
### Deno

Import as ES module

```ts
import { TypedError } from "@think-it-labs/typed-error"
import { TypedError } from "https://deno.land/x/typederror/mod.ts"
```

Then it's possible to create a custom `Error` class extending the `TypedError`

```ts
enum MyErrorType {
Unknown,
HTTP,
Expand All @@ -53,7 +61,7 @@ export class MyError extends TypedError<MyErrorType> {}

```

Now, during error handling code can inspect the `type` error and define behaviour accordingly
Now, during error handling code can inspect the `type` error and define behavior accordingly

```ts
import { MyError, MyErrorType } from "./my-error"
Expand All @@ -66,7 +74,7 @@ export function errorHandling(error: unknown) {
}
case MyErrorType.Unknown:
default: {
// red alert: unknown behaviour
// red alert: unknown behavior
}
}
}
Expand Down
15 changes: 0 additions & 15 deletions jest.config.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.ts → mod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* `TypedError` is a superset of the `Error` class.
* It extends it by allowing the specialisation to a custom _type_.
* It extends it by allowing the specialization to a custom _type_.
*
* **Example:**
* ```ts
Expand All @@ -14,7 +14,7 @@
* ```
*/
export class TypedError<T> extends Error {
/** Specialisation type */
/** Specialization type */
type: T;

constructor(type: T, message?: string, options?: ErrorOptions) {
Expand Down
24 changes: 0 additions & 24 deletions package.json

This file was deleted.

37 changes: 37 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ex. scripts/build_npm.ts
import { build, emptyDir } from "https://deno.land/x/dnt@0.32.1/mod.ts";

await emptyDir("./npm");

await build({
entryPoints: ["./mod.ts"],
outDir: "./npm",
shims: {
deno: true,
},
compilerOptions: {
lib: ["es2022"],
},
package: {
// package.json properties
name: "@think-it-labs/typed-error",
version: Deno.args[0],
description:
"A better JavaScript Error for for Node.js, Deno and the browser.",
license: "MIT",
repository: {
type: "git",
url: "git+https://github.com/think-it-labs/typed-error.git",
},
engines: {
node: ">=16.9",
},
bugs: {
url: "https://github.com/think-it-labs/typed-error/issues",
},
},
});

// post build steps
// Deno.copyFileSync("LICENSE", "npm/LICENSE");
// Deno.copyFileSync("README.md", "npm/README.md");
15 changes: 0 additions & 15 deletions tests/tsconfig.json

This file was deleted.

Loading

0 comments on commit b82dba8

Please sign in to comment.