core-services-sdk 1.0.0 → 1.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-services-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -20,6 +20,8 @@
20
20
  "description": "",
21
21
  "dependencies": {
22
22
  "amqplib": "^0.10.8",
23
+ "http-status": "^2.1.0",
24
+ "node-fetch": "^3.3.2",
23
25
  "uuid": "^11.1.0"
24
26
  },
25
27
  "devDependencies": {
@@ -28,4 +30,4 @@
28
30
  "url": "^0.11.4",
29
31
  "vitest": "^3.2.4"
30
32
  }
31
- }
33
+ }
@@ -0,0 +1,47 @@
1
+ import * as httpStatus from 'http-status'
2
+
3
+ const DEFAULT_ERROR = {
4
+ httpStatusCode: httpStatus.INTERNAL_SERVER_ERROR,
5
+ code: 'UNKNOWN',
6
+ httpStatusText: httpStatus[httpStatus.INTERNAL_SERVER_ERROR],
7
+ }
8
+
9
+ /**
10
+ *
11
+ */
12
+ export class HttpError extends Error {
13
+ /** @type {number} */
14
+ httpStatusCode
15
+ /** @type {string} */
16
+ code
17
+ /** @type {string} */
18
+ httpStatusText
19
+
20
+ /** @type {Array} */
21
+ details
22
+ constructor({
23
+ httpStatusCode,
24
+ code,
25
+ httpStatusText,
26
+ details = [],
27
+ } = DEFAULT_ERROR) {
28
+ super(`${code || httpStatus[httpStatusCode]}`)
29
+ this.httpStatusCode = httpStatusCode
30
+ this.code = code
31
+ this.httpStatusText = httpStatusText
32
+ this.details = details
33
+
34
+ if (typeof Error.captureStackTrace === 'function') {
35
+ Error.captureStackTrace(this, this.constructor)
36
+ }
37
+ }
38
+
39
+ /**
40
+ *
41
+ * @param {object} instance
42
+ * @returns {boolean}
43
+ */
44
+ static isInstanceOf(instance) {
45
+ return instance instanceof HttpError
46
+ }
47
+ }
@@ -0,0 +1,7 @@
1
+ export const HTTP_METHODS = Object.freeze({
2
+ GET: 'GET',
3
+ POST: 'POST',
4
+ PUT: 'PUT',
5
+ PATCH: 'PATCH',
6
+ DELETE: 'DELETE',
7
+ })
@@ -0,0 +1,146 @@
1
+ import fetch from 'node-fetch'
2
+ import httpStatus from 'http-status'
3
+
4
+ import { HttpError } from './HttpError.js'
5
+ import { HTTP_METHODS } from './http-method.js'
6
+
7
+ const JSON_HEADER = {
8
+ 'Content-Type': 'application/json',
9
+ }
10
+
11
+ const isOkStatus = ({ status }) =>
12
+ // Range of response OK
13
+ status >= httpStatus.OK && status < httpStatus.MULTIPLE_CHOICES
14
+
15
+ const checkStatus = async (res) => {
16
+ if (!isOkStatus(res)) {
17
+ const text = await res.text()
18
+ const info = tryConvertJsonResponse(text)
19
+ const { status, statusText } = res
20
+
21
+ throw new HttpError({
22
+ code: status,
23
+ httpStatusCode: status,
24
+ httpStatusText: statusText,
25
+ details: info,
26
+ })
27
+ }
28
+ return res
29
+ }
30
+
31
+ const tryConvertJsonResponse = (responseText) => {
32
+ try {
33
+ const obj = JSON.parse(responseText)
34
+ return obj
35
+ } catch (error) {
36
+ error.responseText = responseText
37
+ throw error
38
+ }
39
+ }
40
+
41
+ const tryGetJsonResponse = async (response) => {
42
+ const text = await response.text()
43
+ const obj = tryConvertJsonResponse(text)
44
+ return obj
45
+ }
46
+
47
+ export const get = async ({ url, headers = {}, credentials = 'include' }) => {
48
+ const response = await fetch(url, {
49
+ method: HTTP_METHODS.GET,
50
+ headers: {
51
+ ...JSON_HEADER,
52
+ ...headers,
53
+ },
54
+ ...(credentials ? { credentials } : {}),
55
+ })
56
+
57
+ await checkStatus(response)
58
+ const data = await tryGetJsonResponse(response)
59
+ return data
60
+ }
61
+
62
+ export const post = async ({ url, body, headers, credentials = 'include' }) => {
63
+ const response = await fetch(url, {
64
+ method: HTTP_METHODS.POST,
65
+ headers: {
66
+ ...JSON_HEADER,
67
+ ...headers,
68
+ },
69
+ body: JSON.stringify(body),
70
+ ...(credentials ? { credentials } : {}),
71
+ })
72
+ await checkStatus(response)
73
+ const data = await tryGetJsonResponse(response)
74
+ return data
75
+ }
76
+
77
+ export const put = async ({
78
+ url,
79
+ body,
80
+ headers = {},
81
+ credentials = 'include',
82
+ }) => {
83
+ const response = await fetch(url, {
84
+ method: HTTP_METHODS.PUT,
85
+ headers: {
86
+ ...JSON_HEADER,
87
+ ...headers,
88
+ },
89
+ body: JSON.stringify(body),
90
+ ...(credentials ? { credentials } : {}),
91
+ })
92
+
93
+ await checkStatus(response)
94
+ const data = await tryGetJsonResponse(response)
95
+ return data
96
+ }
97
+
98
+ export const patch = async ({
99
+ url,
100
+ body,
101
+ headers,
102
+ credentials = 'include',
103
+ }) => {
104
+ const response = await fetch(url, {
105
+ method: HTTP_METHODS.PATCH,
106
+ headers: {
107
+ ...JSON_HEADER,
108
+ ...headers,
109
+ },
110
+ body: JSON.stringify(body),
111
+ ...(credentials ? { credentials } : {}),
112
+ })
113
+
114
+ await checkStatus(response)
115
+ const data = await tryGetJsonResponse(response)
116
+ return data
117
+ }
118
+
119
+ export const deleteApi = async ({
120
+ url,
121
+ body,
122
+ headers,
123
+ credentials = 'include',
124
+ }) => {
125
+ const response = await fetch(url, {
126
+ method: HTTP_METHODS.DELETE,
127
+ headers: {
128
+ ...JSON_HEADER,
129
+ ...headers,
130
+ },
131
+ ...(body ? { body: JSON.stringify(body) } : {}),
132
+ ...(credentials ? { credentials } : {}),
133
+ })
134
+
135
+ await checkStatus(response)
136
+ const data = await tryGetJsonResponse(response)
137
+ return data
138
+ }
139
+
140
+ export const http = {
141
+ get,
142
+ post,
143
+ patch,
144
+ put,
145
+ deleteApi,
146
+ }
package/src/index.js CHANGED
@@ -1 +1,3 @@
1
1
  export * from './rabbit-mq/index.js'
2
+ export * as http from './http/http.js'
3
+ export { HttpError } from './http/HttpError.js'