Skip to content

Commit cecf4ff

Browse files
committed
feat: add fetch middleware for api keys and request init
docs: optimize ts docs
1 parent 8ad4d83 commit cecf4ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2546
-1925
lines changed

package-lock.json

Lines changed: 1523 additions & 1319 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@
6767
"rollup-plugin-node-globals": "^1.4.0",
6868
"rollup-plugin-terser": "^7.0.2",
6969
"stream-http": "^3.2.0",
70-
"typedoc": "^0.22.13",
70+
"typedoc": "^0.22.15",
7171
"typescript": "^4.2.4",
7272
"vite-compatible-readable-stream": "^3.6.0"
7373
},
7474
"scripts": {
7575
"bootstrap": "lerna bootstrap",
7676
"build": "lerna run build",
77-
"build:docs": "rimraf docs && typedoc --tsconfig tsconfig.typedoc.json packages/**/src/index.ts --release-version $(node -p \"require('./lerna.json').version\")",
77+
"build:docs": "rimraf docs && RELEASE_VERSION=$(node -p \"require('./lerna.json').version\") && typedoc --tsconfig tsconfig.typedoc.json packages/**/src/index.ts --release-version $RELEASE_VERSION",
7878
"clean": "lerna clean",
7979
"lerna": "lerna",
8080
"lint": "npm run lint:eslint && npm run lint:prettier",

packages/auth/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@stacks/common": "^4.0.1",
4444
"@stacks/encryption": "^4.0.1",
4545
"@stacks/network": "^4.0.1",
46-
"@stacks/profile": "^4.0.0",
46+
"@stacks/profile": "^4.0.1",
4747
"cross-fetch": "^3.1.4",
4848
"jsontokens": "^3.0.0",
4949
"query-string": "^6.13.1"

packages/auth/src/messages.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
import { Buffer } from '@stacks/common';
2-
import 'cross-fetch/polyfill';
3-
4-
import { TokenSigner, SECP256K1Client } from 'jsontokens';
5-
import { makeUUID4, nextMonth, getGlobalObject } from '@stacks/common';
6-
import { makeDIDFromAddress } from './dids';
1+
import { Buffer, getGlobalObject, makeUUID4, nextMonth } from '@stacks/common';
72
import {
8-
encryptECIES,
93
decryptECIES,
4+
encryptECIES,
105
makeECPrivateKey,
116
publicKeyToAddress,
127
} from '@stacks/encryption';
13-
import { DEFAULT_SCOPE, AuthScope } from './constants';
8+
import { SECP256K1Client, TokenSigner } from 'jsontokens';
9+
import { AuthScope, DEFAULT_SCOPE } from './constants';
10+
import { makeDIDFromAddress } from './dids';
1411

1512
const VERSION = '1.4.0';
1613

packages/auth/src/profile.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { resolveZoneFileToProfile } from '@stacks/profile';
2-
import { fetchPrivate } from '@stacks/common';
32
import { StacksMainnet, StacksNetwork, StacksNetworkName } from '@stacks/network';
43

54
export interface ProfileLookupOptions {
@@ -31,13 +30,13 @@ export function lookupProfile(lookupOptions: ProfileLookupOptions): Promise<Reco
3130
let lookupPromise;
3231
if (options.zoneFileLookupURL) {
3332
const url = `${options.zoneFileLookupURL.replace(/\/$/, '')}/${options.username}`;
34-
lookupPromise = fetchPrivate(url).then(response => response.json());
33+
lookupPromise = network.fetchFn(url).then(response => response.json());
3534
} else {
3635
lookupPromise = network.getNameInfo(options.username);
3736
}
3837
return lookupPromise.then((responseJSON: any) => {
3938
if (responseJSON.hasOwnProperty('zonefile') && responseJSON.hasOwnProperty('address')) {
40-
return resolveZoneFileToProfile(responseJSON.zonefile, responseJSON.address);
39+
return resolveZoneFileToProfile(responseJSON.zonefile, responseJSON.address, network.fetchFn);
4140
} else {
4241
throw new Error(
4342
'Invalid zonefile lookup response: did not contain `address`' + ' or `zonefile` field'

packages/auth/src/provider.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as queryString from 'query-string';
22
import { decodeToken } from 'jsontokens';
3-
import { BLOCKSTACK_HANDLER, getGlobalObject, fetchPrivate } from '@stacks/common';
3+
import { BLOCKSTACK_HANDLER, getGlobalObject } from '@stacks/common';
4+
import { createFetchFn, FetchFn } from '@stacks/network';
45

56
/**
67
* Retrieves the authentication request from the query string
@@ -36,7 +37,10 @@ export function getAuthRequestFromURL() {
3637
* @private
3738
* @ignore
3839
*/
39-
export async function fetchAppManifest(authRequest: string): Promise<any> {
40+
export async function fetchAppManifest(
41+
authRequest: string,
42+
fetchFn: FetchFn = createFetchFn()
43+
): Promise<any> {
4044
if (!authRequest) {
4145
throw new Error('Invalid auth request');
4246
}
@@ -47,7 +51,7 @@ export async function fetchAppManifest(authRequest: string): Promise<any> {
4751
const manifestURI = payload.manifest_uri as string;
4852
try {
4953
// Logger.debug(`Fetching manifest from ${manifestURI}`)
50-
const response = await fetchPrivate(manifestURI);
54+
const response = await fetchFn(manifestURI);
5155
const responseText = await response.text();
5256
const responseJSON = JSON.parse(responseText);
5357
return { ...responseJSON, manifestURI };

packages/auth/src/userSession.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
import { getAddressFromDID } from './dids';
1616
import {
1717
BLOCKSTACK_DEFAULT_GAIA_HUB_URL,
18-
fetchPrivate,
1918
getGlobalObject,
2019
InvalidStateError,
2120
isLaterVersion,
@@ -28,7 +27,7 @@ import { extractProfile } from '@stacks/profile';
2827
import { AuthScope, DEFAULT_PROFILE } from './constants';
2928
import * as queryString from 'query-string';
3029
import { UserData } from './userData';
31-
import { StacksMainnet } from '@stacks/network';
30+
import { createFetchFn, FetchFn, StacksMainnet } from '@stacks/network';
3231
import { protocolEchoReplyDetection } from './protocolEchoDetection';
3332

3433
/**
@@ -214,7 +213,8 @@ export class UserSession {
214213
* if handling the sign in request fails or there was no pending sign in request.
215214
*/
216215
async handlePendingSignIn(
217-
authResponseToken: string = this.getAuthResponseToken()
216+
authResponseToken: string = this.getAuthResponseToken(),
217+
fetchFn: FetchFn = createFetchFn()
218218
): Promise<UserData> {
219219
const sessionData = this.store.getSessionData();
220220

@@ -312,7 +312,7 @@ export class UserSession {
312312
};
313313
const profileURL = tokenPayload.profile_url as string;
314314
if (!userData.profile && profileURL) {
315-
const response = await fetchPrivate(profileURL);
315+
const response = await fetchFn(profileURL);
316316
if (!response.ok) {
317317
// return blank profile if we fail to fetch
318318
userData.profile = Object.assign({}, DEFAULT_PROFILE);

packages/bns/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"dependencies": {
4343
"@stacks/common": "^4.0.1",
4444
"@stacks/network": "^4.0.1",
45-
"@stacks/transactions": "^4.0.0"
45+
"@stacks/transactions": "^4.0.1"
4646
},
4747
"devDependencies": {
4848
"@types/jest": "^26.0.22",

packages/cli/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@
9090
"webpack-bundle-analyzer": "^4.5.0"
9191
},
9292
"dependencies": {
93-
"@stacks/auth": "^4.0.0",
93+
"@stacks/auth": "^4.0.1",
9494
"@stacks/blockchain-api-client": "^0.34.1",
95-
"@stacks/bns": "^4.0.0",
95+
"@stacks/bns": "^4.0.1",
9696
"@stacks/common": "^4.0.1",
9797
"@stacks/network": "^4.0.1",
98-
"@stacks/stacking": "^4.0.0",
99-
"@stacks/storage": "^4.0.0",
100-
"@stacks/transactions": "^4.0.0",
101-
"@stacks/wallet-sdk": "^4.0.0",
98+
"@stacks/stacking": "^4.0.1",
99+
"@stacks/storage": "^4.0.1",
100+
"@stacks/transactions": "^4.0.1",
101+
"@stacks/wallet-sdk": "^4.0.1",
102102
"ajv": "^6.12.3",
103103
"bip32": "^2.0.6",
104104
"bip39": "^3.0.2",

packages/cli/src/network.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import blockstack from 'blockstack';
1+
import { createFetchFn, FetchFn } from '@stacks/network';
22
import * as bitcoin from 'bitcoinjs-lib';
3-
import fetch from 'node-fetch';
4-
5-
import { CLI_CONFIG_TYPE } from './argparse';
6-
3+
import blockstack from 'blockstack';
74
import { BlockstackNetwork } from 'blockstack/lib/network';
5+
import { CLI_CONFIG_TYPE } from './argparse';
86

97
export interface CLI_NETWORK_OPTS {
108
consensusHash: string | null;
@@ -186,15 +184,16 @@ export class CLINetworkAdapter {
186184
getNamespaceBurnAddress(
187185
namespace: string,
188186
useCLI: boolean = true,
189-
receiveFeesPeriod: number = -1
187+
receiveFeesPeriod: number = -1,
188+
fetchFn: FetchFn = createFetchFn()
190189
): Promise<string> {
191190
// override with CLI option
192191
if (this.namespaceBurnAddress && useCLI) {
193192
return new Promise((resolve: any) => resolve(this.namespaceBurnAddress));
194193
}
195194

196195
return Promise.all([
197-
fetch(`${this.legacyNetwork.blockstackAPIUrl}/v1/namespaces/${namespace}`),
196+
fetchFn(`${this.legacyNetwork.blockstackAPIUrl}/v1/namespaces/${namespace}`),
198197
this.legacyNetwork.getBlockHeight(),
199198
])
200199
.then(([resp, blockHeight]: [any, number]) => {
@@ -244,10 +243,10 @@ export class CLINetworkAdapter {
244243
});
245244
}
246245

247-
getBlockchainNameRecord(name: string): Promise<any> {
246+
getBlockchainNameRecord(name: string, fetchFn: FetchFn = createFetchFn()): Promise<any> {
248247
// TODO: send to blockstack.js
249248
const url = `${this.legacyNetwork.blockstackAPIUrl}/v1/blockchains/bitcoin/names/${name}`;
250-
return fetch(url)
249+
return fetchFn(url)
251250
.then(resp => {
252251
if (resp.status !== 200) {
253252
throw new Error(`Bad response status: ${resp.status}`);
@@ -267,10 +266,14 @@ export class CLINetworkAdapter {
267266
});
268267
}
269268

270-
getNameHistory(name: string, page: number): Promise<Record<string, any[]>> {
269+
getNameHistory(
270+
name: string,
271+
page: number,
272+
fetchFn: FetchFn = createFetchFn()
273+
): Promise<Record<string, any[]>> {
271274
// TODO: send to blockstack.js
272275
const url = `${this.legacyNetwork.blockstackAPIUrl}/v1/names/${name}/history?page=${page}`;
273-
return fetch(url)
276+
return fetchFn(url)
274277
.then(resp => {
275278
if (resp.status !== 200) {
276279
throw new Error(`Bad response status: ${resp.status}`);

0 commit comments

Comments
 (0)