@beyonk/http 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,19 @@
1
+ import { Api } from './api/index.js'
2
+ import * as errors from './errors.js'
3
+
4
+ let config
5
+
6
+ function configure (options) {
7
+ config = options
8
+ }
9
+
10
+ function create () {
11
+ if (!config) { throw new Error('Api client must be configured') }
12
+ return new Api(config)
13
+ }
14
+
15
+ export default {
16
+ configure,
17
+ create,
18
+ errors
19
+ }
@@ -0,0 +1,12 @@
1
+ import {
2
+ AccessDeniedError
3
+ } from '../errors.js'
4
+
5
+ const AccessDeniedMixin = superclass => class AccessDenied extends superclass {
6
+ accessDenied (fn) {
7
+ this.handlers[AccessDeniedError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { AccessDeniedMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ BadDataError
3
+ } from '../errors.js'
4
+
5
+ const BadDataMixin = superclass => class BadData extends superclass {
6
+ badData (fn) {
7
+ this.handlers[BadDataError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { BadDataMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ ConflictError
3
+ } from '../errors.js'
4
+
5
+ const ConflictMixin = superclass => class Conflict extends superclass {
6
+ conflict (fn) {
7
+ this.handlers[ConflictError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { ConflictMixin }
@@ -0,0 +1,8 @@
1
+ const DefaultMixin = superclass => class Default extends superclass {
2
+ default (fn) {
3
+ this.defaultHandler = fn
4
+ return this
5
+ }
6
+ }
7
+
8
+ export { DefaultMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ ExpectationFailedError
3
+ } from '../errors.js'
4
+
5
+ const ExpectationFailedMixin = superclass => class ExpectationFailed extends superclass {
6
+ expectationFailed (fn) {
7
+ this.handlers[ExpectationFailedError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { ExpectationFailedMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ ForbiddenError
3
+ } from '../errors.js'
4
+
5
+ const ForbiddenMixin = superclass => class Forbidden extends superclass {
6
+ forbidden (fn) {
7
+ this.handlers[ForbiddenError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { ForbiddenMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ GoneError
3
+ } from '../errors.js'
4
+
5
+ const GoneMixin = superclass => class Gone extends superclass {
6
+ gone (fn) {
7
+ this.handlers[GoneError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { GoneMixin }
@@ -0,0 +1,25 @@
1
+ function getConstructorName (c) {
2
+ return Object.getPrototypeOf(c).constructor.name
3
+ }
4
+
5
+ function findHandlerInReallyUglyWay (localHandlers, globalHandlers, fallbackHandler, e) {
6
+ const constructorName = getConstructorName(e)
7
+ const globalHandlerName = `${constructorName[0].toLowerCase()}${constructorName.slice(1, -5)}`
8
+ return localHandlers[constructorName] ||
9
+ globalHandlers[globalHandlerName] ||
10
+ fallbackHandler
11
+ }
12
+
13
+ const HandleMixin = superclass => class Handle extends superclass {
14
+ get fallbackHandler () {
15
+ return this.defaultHandler || function (e) {
16
+ console.error(getConstructorName(e), e.message, e)
17
+ }
18
+ }
19
+
20
+ handle (e, ctx) {
21
+ return findHandlerInReallyUglyWay(this.handlers, this.options.handlers, this.fallbackHandler, e)(e, ctx)
22
+ }
23
+ }
24
+
25
+ export { HandleMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ NotAcceptableError
3
+ } from '../errors.js'
4
+
5
+ const NotAcceptableMixin = superclass => class NotAcceptable extends superclass {
6
+ notAcceptable (fn) {
7
+ this.handlers[NotAcceptableError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { NotAcceptableMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ NotFoundError
3
+ } from '../errors.js'
4
+
5
+ const NotFoundMixin = superclass => class NotFound extends superclass {
6
+ notFound (fn) {
7
+ this.handlers[NotFoundError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { NotFoundMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ PaymentRequiredError
3
+ } from '../errors.js'
4
+
5
+ const PaymentRequiredMixin = superclass => class PaymentRequired extends superclass {
6
+ paymentRequired (fn) {
7
+ this.handlers[PaymentRequiredError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { PaymentRequiredMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ PreconditionFailedError
3
+ } from '../errors.js'
4
+
5
+ const PreconditionFailedMixin = superclass => class PreconditionFailed extends superclass {
6
+ preconditionFailed (fn) {
7
+ this.handlers[PreconditionFailedError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { PreconditionFailedMixin }
@@ -0,0 +1,12 @@
1
+ import {
2
+ TooManyRequestsError
3
+ } from '../errors.js'
4
+
5
+ const TooManyRequestsMixin = superclass => class TooManyRequests extends superclass {
6
+ tooManyRequests (fn) {
7
+ this.handlers[TooManyRequestsError.name] = fn
8
+ return this
9
+ }
10
+ }
11
+
12
+ export { TooManyRequestsMixin }
package/lib/errors.js ADDED
@@ -0,0 +1,73 @@
1
+ class HttpError extends Error {
2
+ constructor (message, body) {
3
+ super(message)
4
+ this.body = body
5
+ }
6
+ }
7
+
8
+ class AccessDeniedError extends HttpError {
9
+ }
10
+
11
+ class PaymentRequiredError extends HttpError {
12
+ }
13
+
14
+ class ForbiddenError extends HttpError {
15
+ }
16
+
17
+ class NotFoundError extends HttpError {
18
+ }
19
+
20
+ class ConflictError extends HttpError {
21
+ }
22
+
23
+ class ExpectationFailedError extends HttpError {
24
+ }
25
+
26
+ class PreconditionFailedError extends HttpError {
27
+ }
28
+
29
+ class BadDataError extends HttpError {
30
+ }
31
+
32
+ class NotAcceptableError extends HttpError {
33
+ }
34
+
35
+ class GoneError extends HttpError {
36
+ }
37
+
38
+ class TooManyRequestsError extends HttpError {
39
+ }
40
+
41
+ const mapping = {
42
+ 401: AccessDeniedError,
43
+ 402: PaymentRequiredError,
44
+ 403: ForbiddenError,
45
+ 404: NotFoundError,
46
+ 406: NotAcceptableError,
47
+ 409: ConflictError,
48
+ 410: GoneError,
49
+ 412: PreconditionFailedError,
50
+ 417: ExpectationFailedError,
51
+ 422: BadDataError,
52
+ 429: TooManyRequestsError
53
+ }
54
+
55
+ function byCode (code) {
56
+ return mapping[code] || HttpError
57
+ }
58
+
59
+ export {
60
+ byCode,
61
+ AccessDeniedError,
62
+ PaymentRequiredError,
63
+ ForbiddenError,
64
+ NotFoundError,
65
+ NotAcceptableError,
66
+ HttpError,
67
+ GoneError,
68
+ ConflictError,
69
+ PreconditionFailedError,
70
+ ExpectationFailedError,
71
+ BadDataError,
72
+ TooManyRequestsError
73
+ }
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@beyonk/http",
3
+ "version": "12.0.0",
4
+ "description": "An isomorphic http client for Svelte apps",
5
+ "main": "lib/entrypoint.js",
6
+ "module": "lib/entrypoint.js",
7
+ "type": "module",
8
+ "directories": {
9
+ "lib": "lib"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/beyonk-adventures/http.git"
14
+ },
15
+ "devDependencies": {
16
+ "@beyonk/eslint-config": "^4.2.0",
17
+ "@beyonk/esm": "^4.0.1",
18
+ "@hapi/code": "^5.3.1",
19
+ "eslint": "^7.2.0",
20
+ "mocha": "^9.0.2",
21
+ "sinon": "^7.5.0"
22
+ },
23
+ "eslintConfig": {
24
+ "extends": "@beyonk",
25
+ "globals": {
26
+ "beforeEach": true,
27
+ "afterEach": true,
28
+ "describe": true,
29
+ "context": true,
30
+ "it": true
31
+ }
32
+ },
33
+ "keywords": [
34
+ "isomorphic",
35
+ "fetch",
36
+ "sapper",
37
+ "sveltekit",
38
+ "svelte",
39
+ "http-client",
40
+ "http",
41
+ "https"
42
+ ],
43
+ "author": "Antony Jones",
44
+ "license": "MIT",
45
+ "volta": {
46
+ "node": "18.15.0"
47
+ },
48
+ "packageManager": "pnpm@8.0.0",
49
+ "scripts": {
50
+ "test": "mocha './!(node_modules)/**/**.+(spec).js'",
51
+ "lint": "eslint lib --ext .js"
52
+ }
53
+ }