add dist
This commit is contained in:
parent
849b917d50
commit
b8ecf08f34
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -118,7 +118,7 @@ out
|
||||||
# Nuxt.js build / generate output
|
# Nuxt.js build / generate output
|
||||||
|
|
||||||
.nuxt
|
.nuxt
|
||||||
dist
|
|
||||||
|
|
||||||
# Gatsby files
|
# Gatsby files
|
||||||
|
|
||||||
|
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 Anton Nesterov
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
14
README.md
14
README.md
|
@ -7,7 +7,7 @@ It makes the control flow similar to that of Golang, but doesn't force you to ma
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
bun add github:nesterow/grip
|
bun add github:nesterow/grip # or pnpm
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
@ -24,20 +24,20 @@ import { grip } from '@nesterow/grip';
|
||||||
The result can be handled as an object: `{value, status, Ok(), Fail(), Of(type)}`
|
The result can be handled as an object: `{value, status, Ok(), Fail(), Of(type)}`
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const fetchResult = await grip(
|
const res = await grip(
|
||||||
fetch('https://api.example.com')
|
fetch('https://api.example.com')
|
||||||
);
|
);
|
||||||
|
|
||||||
if (fetchResult.Fail()) {
|
if (res.Fail()) {
|
||||||
handleErrorProperly();
|
handleErrorProperly();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const jsonResult = await grip(
|
const json = await grip(
|
||||||
res.value.json()
|
res.value.json()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (jsonResult.Of(SyntaxError)) {
|
if (json.Of(SyntaxError)) {
|
||||||
handleJsonParseError();
|
handleJsonParseError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ if (jsonResult.Of(SyntaxError)) {
|
||||||
The result can also be received as a tuple if you want to handle errors in Go'ish style:
|
The result can also be received as a tuple if you want to handle errors in Go'ish style:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const [response, fetchStatus] = await grip(
|
const [res, fetchStatus] = await grip(
|
||||||
fetch('https://api.example.com')
|
fetch('https://api.example.com')
|
||||||
);
|
);
|
||||||
if (fetchStatus.Fail()) {
|
if (fetchStatus.Fail()) {
|
||||||
|
@ -58,7 +58,7 @@ if (fetchStatus.Fail()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const [json, parseStatus] = await grip(
|
const [json, parseStatus] = await grip(
|
||||||
response.json()
|
res.json()
|
||||||
);
|
);
|
||||||
if (parseStatus.Of(SyntaxError)) {
|
if (parseStatus.Of(SyntaxError)) {
|
||||||
handleJsonParseError();
|
handleJsonParseError();
|
||||||
|
|
78
dist/grip.js
vendored
Normal file
78
dist/grip.js
vendored
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
// grip.ts
|
||||||
|
function grip(action) {
|
||||||
|
if (action instanceof Promise) {
|
||||||
|
return promise(action);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const result = action();
|
||||||
|
if (result instanceof Promise) {
|
||||||
|
return promise(result);
|
||||||
|
}
|
||||||
|
return new Result(result, new Ok);
|
||||||
|
} catch (err) {
|
||||||
|
return new Result(null, Err.fromCatch(err));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Err extends Error {
|
||||||
|
Ok() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Fail() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Of(cls) {
|
||||||
|
return this.cause instanceof cls || this instanceof cls;
|
||||||
|
}
|
||||||
|
static fromCatch(error) {
|
||||||
|
const e = new Err(typeof error === "string" ? error : error.message);
|
||||||
|
e.cause = error;
|
||||||
|
e.stack = error.stack;
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Ok extends Error {
|
||||||
|
Ok() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Fail() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Of(cls) {
|
||||||
|
return this instanceof cls;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Result extends Array {
|
||||||
|
0;
|
||||||
|
1;
|
||||||
|
constructor(result, status) {
|
||||||
|
super(2);
|
||||||
|
this[0] = result;
|
||||||
|
this[1] = status;
|
||||||
|
}
|
||||||
|
get value() {
|
||||||
|
return this[0];
|
||||||
|
}
|
||||||
|
get status() {
|
||||||
|
return this[1];
|
||||||
|
}
|
||||||
|
Ok() {
|
||||||
|
return this[1].Ok();
|
||||||
|
}
|
||||||
|
Fail() {
|
||||||
|
return this[1].Fail();
|
||||||
|
}
|
||||||
|
Of(impl) {
|
||||||
|
return this[1].Of(impl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var promise = (result) => {
|
||||||
|
return result.then((res) => new Result(res, new Ok)).catch((err) => new Result(null, Err.fromCatch(err)));
|
||||||
|
};
|
||||||
|
export {
|
||||||
|
grip,
|
||||||
|
Ok,
|
||||||
|
Err
|
||||||
|
};
|
Loading…
Reference in a new issue