hivesigner 3.3.4 → 4.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.
@@ -2,16 +2,14 @@ import { Client } from './client'
2
2
  import { ClientConfig } from './types'
3
3
  import { API_URL, BASE_URL } from './consts'
4
4
  import * as utilities from './utilities'
5
- import * as crossFetch from 'cross-fetch'
6
5
 
7
6
  jest.mock('./utilities')
8
- jest.mock('cross-fetch')
9
7
 
10
8
  describe('Client', function () {
11
9
  let instance: Client
12
10
  let isBrowserMock: jest.Mock
13
- let crossFetchMock: jest.Mock
14
- let crossFetchResponseMock: { status: string | number, json: () => {} }
11
+ let fetchMock: jest.Mock
12
+ let fetchResponseMock: { status: string | number, json: () => {} }
15
13
 
16
14
  beforeEach(() => {
17
15
  instance = new Client({
@@ -26,12 +24,12 @@ describe('Client', function () {
26
24
  isBrowserMock = utilities.isBrowser as jest.Mock
27
25
  isBrowserMock.mockReturnValue(false)
28
26
 
29
- crossFetchResponseMock = {
27
+ fetchResponseMock = {
30
28
  status: 200,
31
29
  json: async () => ({ error: '' })
32
30
  }
33
- crossFetchMock = crossFetch.fetch as jest.Mock
34
- crossFetchMock.mockImplementation(async (...args: any[]) => crossFetchResponseMock)
31
+ fetchMock = jest.fn().mockImplementation(async (...args: any[]) => fetchResponseMock)
32
+ global.fetch = fetchMock
35
33
  })
36
34
 
37
35
  it('should create instance', function () {
@@ -101,7 +99,7 @@ describe('Client', function () {
101
99
 
102
100
  it('should return promise resolve on send call', async function () {
103
101
  const response = await instance.send('me', 'POST', {})
104
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/me`,{
102
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/me`,{
105
103
  method: 'POST',
106
104
  headers: {
107
105
  Accept: 'application/json, text/plain, */*',
@@ -110,11 +108,11 @@ describe('Client', function () {
110
108
  },
111
109
  body: JSON.stringify({})
112
110
  })
113
- expect(response).toStrictEqual(await crossFetchResponseMock.json())
111
+ expect(response).toStrictEqual(await fetchResponseMock.json())
114
112
  })
115
113
 
116
114
  it('should return promise error on send call with if 200 status', async function () {
117
- crossFetchResponseMock = {
115
+ fetchResponseMock = {
118
116
  status: 'not_200_OK',
119
117
  json: async () => ({ error: '' })
120
118
  }
@@ -124,12 +122,12 @@ describe('Client', function () {
124
122
  console.error('Client.send not threw error')
125
123
  expect(true).toBe(false)
126
124
  } catch (failureResponse) {
127
- expect(failureResponse).toStrictEqual(await crossFetchResponseMock.json())
125
+ expect(failureResponse).toStrictEqual(await fetchResponseMock.json())
128
126
  }
129
127
  })
130
128
 
131
129
  it('should return promise error on send call if has json error', async function () {
132
- crossFetchResponseMock = {
130
+ fetchResponseMock = {
133
131
  status: 200,
134
132
  json: async () => ({ error: 'I have an error' })
135
133
  }
@@ -140,7 +138,7 @@ describe('Client', function () {
140
138
  expect(true).toBe(false)
141
139
  } catch (failureResponse) {
142
140
  expect(failureResponse).toBeTruthy()
143
- expect(failureResponse).toStrictEqual(await crossFetchResponseMock.json())
141
+ expect(failureResponse).toStrictEqual(await fetchResponseMock.json())
144
142
  }
145
143
  })
146
144
 
@@ -149,14 +147,14 @@ describe('Client', function () {
149
147
  const response = await instance.send('me', 'POST', {}, callback)
150
148
 
151
149
  expect(callback).toHaveBeenCalled()
152
- expect(callback).toHaveBeenCalledWith(null, await crossFetchResponseMock.json())
150
+ expect(callback).toHaveBeenCalledWith(null, await fetchResponseMock.json())
153
151
  expect(callback).lastReturnedWith('results')
154
152
  expect(response).toBe('results')
155
153
  })
156
154
 
157
155
  it('should return promise with failure results on send call with callback', async function () {
158
156
  const callback = jest.fn().mockReturnValue('results')
159
- crossFetchResponseMock = {
157
+ fetchResponseMock = {
160
158
  status: 'not_200_OK',
161
159
  json: async () => ({ error: '' })
162
160
  }
@@ -164,14 +162,14 @@ describe('Client', function () {
164
162
 
165
163
 
166
164
  expect(callback).toHaveBeenCalled()
167
- expect(callback).toHaveBeenCalledWith(await crossFetchResponseMock.json(), null)
165
+ expect(callback).toHaveBeenCalledWith(await fetchResponseMock.json(), null)
168
166
  expect(callback).lastReturnedWith('results')
169
167
  expect(response).toBe('results')
170
168
  })
171
169
 
172
170
  it('should return promise on broadcast call', async function () {
173
171
  const response = await instance.broadcast([])
174
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
172
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
175
173
  method: 'POST',
176
174
  headers: {
177
175
  Accept: 'application/json, text/plain, */*',
@@ -185,7 +183,7 @@ describe('Client', function () {
185
183
 
186
184
  it('should return promise on updateUserMetadata call', async function () {
187
185
  const response = await instance.updateUserMetadata({ username: '' })
188
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/me`,{
186
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/me`,{
189
187
  method: 'PUT',
190
188
  headers: {
191
189
  Accept: 'application/json, text/plain, */*',
@@ -199,7 +197,7 @@ describe('Client', function () {
199
197
 
200
198
  it('should send request and return client instance on revokeToken call', async function () {
201
199
  const response = await instance.revokeToken()
202
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/oauth2/token/revoke`,{
200
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/oauth2/token/revoke`,{
203
201
  method: 'POST',
204
202
  headers: {
205
203
  Accept: 'application/json, text/plain, */*',
@@ -213,7 +211,7 @@ describe('Client', function () {
213
211
 
214
212
  it('should send request and return promise on claimRewardBalance call', async function () {
215
213
  const response = await instance.claimRewardBalance('', '', '', '')
216
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
214
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
217
215
  method: 'POST',
218
216
  headers: {
219
217
  Accept: 'application/json, text/plain, */*',
@@ -234,7 +232,7 @@ describe('Client', function () {
234
232
 
235
233
  it('should send request and return promise on custom json call', async function () {
236
234
  const response = await instance.customJson('', '', '', {})
237
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
235
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
238
236
  method: 'POST',
239
237
  headers: {
240
238
  Accept: 'application/json, text/plain, */*',
@@ -255,7 +253,7 @@ describe('Client', function () {
255
253
 
256
254
  it('should send request and return promise on ignore call', async function () {
257
255
  const response = await instance.ignore('', '')
258
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
256
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
259
257
  method: 'POST',
260
258
  headers: {
261
259
  Accept: 'application/json, text/plain, */*',
@@ -280,7 +278,7 @@ describe('Client', function () {
280
278
 
281
279
  it('should send request and return promise on unfollow call', async function () {
282
280
  const response = await instance.unfollow('', '')
283
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
281
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
284
282
  method: 'POST',
285
283
  headers: {
286
284
  Accept: 'application/json, text/plain, */*',
@@ -305,7 +303,7 @@ describe('Client', function () {
305
303
 
306
304
  it('should send request and return promise on follow call', async function () {
307
305
  const response = await instance.follow('', '')
308
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
306
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
309
307
  method: 'POST',
310
308
  headers: {
311
309
  Accept: 'application/json, text/plain, */*',
@@ -330,7 +328,7 @@ describe('Client', function () {
330
328
 
331
329
  it('should send request and return promise on reblog call', async function () {
332
330
  const response = await instance.reblog('', '', '')
333
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
331
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
334
332
  method: 'POST',
335
333
  headers: {
336
334
  Accept: 'application/json, text/plain, */*',
@@ -355,7 +353,7 @@ describe('Client', function () {
355
353
 
356
354
  it('should send request and return promise on deleteComment call', async function () {
357
355
  const response = await instance.deleteComment('', '')
358
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
356
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
359
357
  method: 'POST',
360
358
  headers: {
361
359
  Accept: 'application/json, text/plain, */*',
@@ -374,7 +372,7 @@ describe('Client', function () {
374
372
 
375
373
  it('should send request and return promise on comment call', async function () {
376
374
  let response = await instance.comment('', '', '', '', '', {}, {})
377
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
375
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
378
376
  method: 'POST',
379
377
  headers: {
380
378
  Accept: 'application/json, text/plain, */*',
@@ -396,7 +394,7 @@ describe('Client', function () {
396
394
  expect(response).toBeTruthy()
397
395
 
398
396
  response = await instance.comment('', '', '', '', '', {}, 'meta')
399
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
397
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
400
398
  method: 'POST',
401
399
  headers: {
402
400
  Accept: 'application/json, text/plain, */*',
@@ -420,7 +418,7 @@ describe('Client', function () {
420
418
 
421
419
  it('should send request and return promise on vote call', async function () {
422
420
  const response = await instance.vote('', '', '', 0)
423
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
421
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/broadcast`,{
424
422
  method: 'POST',
425
423
  headers: {
426
424
  Accept: 'application/json, text/plain, */*',
@@ -441,7 +439,7 @@ describe('Client', function () {
441
439
 
442
440
  it('should send request and return promise on me call', async function () {
443
441
  const response = await instance.me()
444
- expect(crossFetch.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/me`,{
442
+ expect(global.fetch).toHaveBeenCalledWith(`${instance.apiURL}/api/me`,{
445
443
  method: 'POST',
446
444
  headers: {
447
445
  Accept: 'application/json, text/plain, */*',
package/src/client.ts CHANGED
@@ -1,8 +1,6 @@
1
- import fetch from 'cross-fetch'
2
- import { CallbackFunction, ClientConfig, LoginOptions, SendResponse } from './types'
1
+ import { CallbackFunction, ClientConfig, LoginOptions, Operation, SendResponse } from './types'
3
2
  import { API_URL, BASE_URL } from './consts'
4
3
  import { isBrowser } from './utilities'
5
- import { Operation } from '@hiveio/dhive'
6
4
 
7
5
  export class Client {
8
6
  public apiURL: string
@@ -293,7 +291,13 @@ export class Client {
293
291
  }
294
292
 
295
293
  private async makeRequest(url: string, method: string, body: any): Promise<SendResponse> {
296
- const response = await fetch(url, {
294
+ if (typeof globalThis.fetch !== 'function') {
295
+ throw new Error(
296
+ 'hivesigner: fetch is not available. Node.js 18+ or a modern browser is required. ' +
297
+ 'If you are in an older environment, provide a global fetch polyfill.'
298
+ )
299
+ }
300
+ const response = await globalThis.fetch(url, {
297
301
  method,
298
302
  headers: {
299
303
  Accept: 'application/json, text/plain, */*',
@@ -1,4 +1,4 @@
1
- import { Operation } from '@hiveio/dhive'
1
+ import { Operation } from '../types'
2
2
  import * as hive from 'hive-uri'
3
3
  import * as utilities from '../utilities'
4
4
  import { CallbackFunction } from '../types'
@@ -1,8 +1,7 @@
1
1
  import { BETA_URL } from '../consts'
2
2
  import { encodeOp, Parameters } from 'hive-uri'
3
3
  import { isBrowser } from '../utilities'
4
- import { CallbackFunction } from '../types'
5
- import { Operation } from '@hiveio/dhive'
4
+ import { CallbackFunction, Operation } from '../types'
6
5
 
7
6
  export function sendOperation(op: Operation, params: Parameters, cb: CallbackFunction): string | void {
8
7
  const uri = encodeOp(op, params)
@@ -1,4 +1,4 @@
1
- import { Operation } from '@hiveio/dhive'
1
+ import { Operation } from '../types'
2
2
  import * as hive from 'hive-uri'
3
3
  import * as utilities from '../utilities'
4
4
  import { CallbackFunction } from '../types'
@@ -1,8 +1,7 @@
1
1
  import { encodeOps, Parameters } from 'hive-uri'
2
2
  import { isBrowser } from '../utilities'
3
3
  import { BETA_URL } from '../consts'
4
- import { CallbackFunction } from '../types'
5
- import { Operation } from '@hiveio/dhive'
4
+ import { CallbackFunction, Operation } from '../types'
6
5
 
7
6
  export function sendOperations(ops: Operation[], params: Parameters, cb: CallbackFunction): string | void {
8
7
  const uri = encodeOps(ops, params)
@@ -1,5 +1,5 @@
1
1
  import { sendTransaction } from './send-transaction.method'
2
- import { Transaction } from '@hiveio/dhive'
2
+ import { Transaction } from '../types'
3
3
  import * as hive from 'hive-uri'
4
4
  import * as utilities from '../utilities'
5
5
  import { CallbackFunction } from '../types'
@@ -1,8 +1,7 @@
1
1
  import { encodeTx, Parameters } from 'hive-uri'
2
2
  import { BETA_URL } from '../consts'
3
3
  import { isBrowser } from '../utilities'
4
- import { CallbackFunction } from '../types'
5
- import { Transaction } from '@hiveio/dhive'
4
+ import { CallbackFunction, Transaction } from '../types'
6
5
 
7
6
  export function sendTransaction(tx: Transaction, params: Parameters, cb: CallbackFunction): string | void {
8
7
  const uri = encodeTx(tx, params)
@@ -1,5 +1,10 @@
1
1
  const hs = require('../lib/index');
2
2
 
3
+ global.fetch = jest.fn().mockResolvedValue({
4
+ status: 200,
5
+ json: async () => ({ error: '' })
6
+ });
7
+
3
8
  describe('Require import testing', function () {
4
9
  it('should be instance of Client and call me', function () {
5
10
  const client = new hs.Client({ accessToken: 'some_wrong_access_token' });
@@ -8,4 +13,4 @@ describe('Require import testing', function () {
8
13
  const me = client.me();
9
14
  expect(me).toBeInstanceOf(Promise);
10
15
  });
11
- });
16
+ });
@@ -0,0 +1,12 @@
1
+ export interface Operation {
2
+ 0: string
3
+ 1: { [key: string]: any }
4
+ }
5
+
6
+ export interface Transaction {
7
+ ref_block_num: number
8
+ ref_block_prefix: number
9
+ expiration: string
10
+ operations: Operation[]
11
+ extensions: any[]
12
+ }
@@ -3,3 +3,4 @@ export * from './sign-errors.interface'
3
3
  export * from './callback-function.type'
4
4
  export * from './login-options.interface'
5
5
  export * from './send-response.type'
6
+ export * from './hive.types'
package/tsconfig.json CHANGED
@@ -6,8 +6,10 @@
6
6
  "allowSyntheticDefaultImports": true,
7
7
  "sourceMap": true,
8
8
  "module": "commonjs",
9
- "target": "es5",
9
+ "target": "ES2017",
10
+ "lib": ["ES2017", "DOM"],
10
11
  "allowJs": false,
12
+ "skipLibCheck": true,
11
13
  "declaration": true,
12
14
  "rootDir": "src"
13
15
  },
package/webpack.config.js CHANGED
@@ -15,7 +15,14 @@ module.exports = {
15
15
  rules: [
16
16
  {
17
17
  test: /\.ts$/,
18
- loader: 'babel-loader',
18
+ loader: 'ts-loader',
19
+ exclude: /node_modules/,
20
+ options: {
21
+ compilerOptions: {
22
+ declaration: false,
23
+ sourceMap: false,
24
+ }
25
+ },
19
26
  },
20
27
  ]
21
28
  },
package/.babelrc DELETED
@@ -1,8 +0,0 @@
1
- {
2
- "presets": ["@babel/preset-env", "@babel/preset-typescript"],
3
- "plugins": [
4
- "@babel/plugin-proposal-object-rest-spread",
5
- "@babel/plugin-proposal-class-properties",
6
- "@babel/plugin-transform-runtime"
7
- ]
8
- }