Skip to content

Commit adce261

Browse files
committed
docs: optimize ts docs
1 parent ab648c9 commit adce261

File tree

7 files changed

+59
-36
lines changed

7 files changed

+59
-36
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
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/common/src/fetchUtil.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,36 @@ const defaultFetchOpts: RequestInit = {
77
referrerPolicy: 'origin', // Use origin value for referrer policy
88
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
99
};
10-
/*
10+
11+
/**
1112
* Get fetch options
12-
* @return fetchOptions
13+
* @category Network
1314
*/
1415
export const getFetchOptions = () => {
1516
return defaultFetchOpts;
1617
};
17-
/*
18-
* Set fetch options
19-
* Users can change default referrer as well as other options when fetch is used internally by stacks.js libraries or from server side
18+
19+
/**
20+
* Sets global fetch options for stacks.js network calls.
21+
*
2022
* @example
21-
* Reference: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
22-
* setFetchOptions({ referrer: 'no-referrer', referrerPolicy: 'no-referrer', ... other options as per above reference });
23-
* Now all the subsequent fetchPrivate will use above options
24-
* @return fetchOptions
23+
* Users can change the default referrer as well as other options when fetch is used internally by stacks.js:
24+
* ```
25+
* setFetchOptions({ referrer: 'no-referrer', referrerPolicy: 'no-referrer', ...otherRequestOptions });
26+
* ```
27+
* After calling {@link setFetchOptions} all subsequent network calls will use the specified options above.
28+
*
29+
* @see MDN Request: https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
30+
* @returns global fetch options after merging with previous options (or defaults)
31+
* @category Network
32+
* @related {@link getFetchOptions}
2533
*/
26-
export const setFetchOptions = (ops: RequestInit) => {
34+
export const setFetchOptions = (ops: RequestInit): RequestInit => {
2735
return Object.assign(defaultFetchOpts, ops);
2836
};
2937

30-
/** @ignore */
31-
export async function fetchPrivate(input: RequestInfo, init?: RequestInit): Promise<Response> {
38+
/** @internal */
39+
export async function fetchWrapper(input: RequestInfo, init?: RequestInit): Promise<Response> {
3240
const fetchOpts = {};
3341
// Use the provided options in request options along with default or user provided values
3442
Object.assign(fetchOpts, init, defaultFetchOpts);
@@ -81,11 +89,16 @@ export function hostMatches(host: string, pattern: string | RegExp) {
8189
* @example
8290
* ```
8391
* const apiMiddleware = createApiKeyMiddleware("example_e8e044a3_41d8b0fe_3dd3988ef302");
92+
<<<<<<< HEAD
8493
* const fetchFn = createFetchFn(apiMiddleware);
8594
* const network = new StacksMainnet({ fetchFn });
8695
* ```
8796
* @category Network
8897
* @related {@link createFetchFn}, {@link StacksNetwork}
98+
=======
99+
* ```
100+
* @category Network
101+
>>>>>>> 48226115 (docs: optimize ts docs)
89102
*/
90103
export function createApiKeyMiddleware({
91104
apiKey,
@@ -105,7 +118,7 @@ export function createApiKeyMiddleware({
105118
}
106119

107120
function argsForCreateFetchFn(args: any[]): { middlewares: FetchMiddleware[]; fetchLib: FetchFn } {
108-
let fetchLib: FetchFn = fetch;
121+
let fetchLib: FetchFn = fetchWrapper;
109122
let middlewares: FetchMiddleware[] = [];
110123
if (args.length > 0 && typeof args[0] === 'function') {
111124
fetchLib = args.shift();
@@ -116,6 +129,16 @@ function argsForCreateFetchFn(args: any[]): { middlewares: FetchMiddleware[]; fe
116129
return { middlewares, fetchLib };
117130
}
118131

132+
/**
133+
* Creates a new network fetching function, which combines an optional fetch-compatible library with optional middlware.
134+
* @example
135+
* ```
136+
* const customFetch = createFetchFn(someMiddleware)
137+
* const customFetch = createFetchFn(fetch, someMiddleware)
138+
* const customFetch = createFetchFn(fetch, middlewareA, middlewareB)
139+
* ```
140+
* @category Network
141+
*/
119142
export function createFetchFn(fetchLib: FetchFn, ...middleware: FetchMiddleware[]): FetchFn;
120143
export function createFetchFn(...middleware: FetchMiddleware[]): FetchFn;
121144
export function createFetchFn(...args: any[]): FetchFn {
@@ -124,6 +147,7 @@ export function createFetchFn(...args: any[]): FetchFn {
124147

125148
const fetchFn = async (url: string, init?: RequestInit | undefined): Promise<Response> => {
126149
let fetchParams = { url, init: init ?? {} };
150+
127151
for (const middleware of middlewares) {
128152
if (typeof middleware.pre !== 'function') continue;
129153
const result = await Promise.resolve(

packages/common/tests/fetch.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
import { fetchPrivate, getFetchOptions, setFetchOptions } from '../src'
2-
import fetchMock from "jest-fetch-mock";
1+
import { fetchWrapper, getFetchOptions, setFetchOptions } from '../src';
2+
import fetchMock from 'jest-fetch-mock';
33

44
test('Verify fetch private options', async () => {
5-
const defaultOptioins = getFetchOptions();
5+
const defaultOptioins = getFetchOptions();
66

77
expect(defaultOptioins).toEqual({ referrerPolicy: 'origin' });
88

99
// Override default options when fetchPrivate is called internally by other stacks.js libraries like transactions or from server side
1010
// This is for developers as they cannot directly pass options directly in fetchPrivate
11-
const modifiedOptions: RequestInit= { referrer: 'http://test.com', referrerPolicy: 'same-origin' };
11+
const modifiedOptions: RequestInit = {
12+
referrer: 'http://test.com',
13+
referrerPolicy: 'same-origin',
14+
};
1215

1316
// Developers can set fetch options globally one time specifically when fetchPrivate is used internally by stacks.js libraries
1417
setFetchOptions(modifiedOptions);
@@ -18,11 +21,10 @@ test('Verify fetch private options', async () => {
1821
// Browser will replace about:client with actual url but it will not be visible in test case
1922
fetchMock.mockOnce(`{ status: 'success'}`, { headers: modifiedOptions as any });
2023

21-
const result = await fetchPrivate('https://example.com');
24+
const result = await fetchWrapper('https://example.com');
2225

2326
// Verify the request options
2427
expect(result.status).toEqual(200);
2528
expect(result.headers.get('referrer')).toEqual(modifiedOptions.referrer);
2629
expect(result.headers.get('referrerPolicy')).toEqual(modifiedOptions.referrerPolicy);
27-
})
28-
30+
});

packages/keychain/src/identity.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { makeAuthResponse } from '@stacks/auth';
2-
import { FetchFn, createFetchFn } from '@stacks/common';
2+
import { createFetchFn, FetchFn } from '@stacks/common';
33
import { getPublicKeyFromPrivate, publicKeyToAddress } from '@stacks/encryption';
44
import { bip32 } from 'bitcoinjs-lib';
55
import { Identity as IdentifyInterface, Profile } from './common';
66
import IdentityAddressOwnerNode from './nodes/identity-address-owner-node';
77
import { DEFAULT_PROFILE, fetchProfile, signAndUploadProfile } from './profiles';
88
import { getProfileURLFromZoneFile, IdentityKeyPair } from './utils';
9-
import { fetchPrivate } from '@stacks/common';
109
import {
1110
connectToGaiaHubWithConfig,
1211
DEFAULT_GAIA_HUB,

packages/transactions/src/utils.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { Buffer, fetchPrivate } from '@stacks/common';
2-
import { sha256, sha512 } from 'sha.js';
3-
import { ClarityValue, serializeCV } from './clarity';
4-
import RIPEMD160 from 'ripemd160-min';
1+
import { bytesToHex } from '@noble/hashes/utils';
52
import { utils } from '@noble/secp256k1';
6-
import { deserializeCV } from './clarity';
3+
import { Buffer, with0x } from '@stacks/common';
74
import { c32addressDecode } from 'c32check';
85
import lodashCloneDeep from 'lodash.clonedeep';
9-
import { with0x } from '@stacks/common';
10-
import { bytesToHex } from '@noble/hashes/utils';
6+
import RIPEMD160 from 'ripemd160-min';
7+
import { sha256, sha512 } from 'sha.js';
8+
import { ClarityValue, deserializeCV, serializeCV } from './clarity';
119

1210
/**
1311
* Use utils.randomBytes to replace randombytes dependency
@@ -197,9 +195,6 @@ export function isClarityName(name: string) {
197195
return regex.test(name) && name.length < 128;
198196
}
199197

200-
/** @ignore */
201-
export { fetchPrivate };
202-
203198
/**
204199
* Converts a clarity value to a hex encoded string with `0x` prefix
205200
* @param {ClarityValue} cv - the clarity value to convert

tsconfig.typedoc.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
"theme": "stacks",
1313
"excludePrivate": true,
1414
"excludeProtected": true,
15-
"gitRevision": "master",
1615
"listInvalidSymbolLinks": true,
16+
"categoryOrder": [
17+
"*",
18+
"Other"
19+
],
1720
// theme and plugin options
1821
"external-modulemap": ".*\/packages\/([\\w\\-_]+)\/",
1922
"package-prefix": "@stacks",

0 commit comments

Comments
 (0)