-
Notifications
You must be signed in to change notification settings - Fork 85
feat: allow no persist for Custom Data Access #1541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kevindavee
merged 11 commits into
master
from
feat/allow-no-persist-for-custom-data-access
Jan 15, 2025
+458
−246
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
161c863
feat: allow no persist for Custom Data Access
kevindavee 952184d
deprecate NoPersistHttpDataAccess + handle no persistence inside Data…
kevindavee b537579
fix test
kevindavee 08b410f
fix default value
kevindavee 3759082
fix request-client.js test
kevindavee 8c60f1a
create NoPersistDataWrite class
kevindavee 48d7654
fix test
kevindavee dcf76d9
remove persist flag
kevindavee 77570b5
remove http-metamask-data-access-write
kevindavee f68fbff
fix
kevindavee 7f36372
change naming
kevindavee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { DataAccessTypes, StorageTypes } from '@requestnetwork/types'; | ||
import { EventEmitter } from 'events'; | ||
|
||
export class NoPersistDataWrite implements DataAccessTypes.IDataWrite { | ||
async initialize(): Promise<void> { | ||
return; | ||
} | ||
|
||
async close(): Promise<void> { | ||
return; | ||
} | ||
|
||
async persistTransaction( | ||
transaction: DataAccessTypes.ITransaction, | ||
channelId: string, | ||
topics?: string[] | undefined, | ||
): Promise<DataAccessTypes.IReturnPersistTransaction> { | ||
kevindavee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const eventEmitter = new EventEmitter() as DataAccessTypes.PersistTransactionEmitter; | ||
|
||
const result: DataAccessTypes.IReturnPersistTransaction = Object.assign(eventEmitter, { | ||
meta: { | ||
topics: topics || [], | ||
transactionStorageLocation: '', | ||
storageMeta: { | ||
state: StorageTypes.ContentState.PENDING, | ||
timestamp: Date.now() / 1000, | ||
}, | ||
}, | ||
result: {}, | ||
}); | ||
|
||
// Emit confirmation instantly since data is not going to be persisted | ||
result.emit('confirmed', result); | ||
return result; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
packages/request-client.js/src/http-data-access-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { ClientTypes } from '@requestnetwork/types'; | ||
import { retry } from '@requestnetwork/utils'; | ||
import httpConfigDefaults from './http-config-defaults'; | ||
import { stringify } from 'qs'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const packageJson = require('../package.json'); | ||
export type NodeConnectionConfig = { baseURL: string; headers: Record<string, string> }; | ||
|
||
export class HttpDataAccessConfig { | ||
/** | ||
* Configuration that overrides http-config-defaults, | ||
* @see httpConfigDefaults for the default configuration. | ||
*/ | ||
public httpConfig: ClientTypes.IHttpDataAccessConfig; | ||
|
||
/** | ||
* Configuration that will be sent at each request. | ||
*/ | ||
public nodeConnectionConfig: NodeConnectionConfig; | ||
|
||
constructor( | ||
{ | ||
httpConfig, | ||
nodeConnectionConfig, | ||
}: { | ||
httpConfig?: Partial<ClientTypes.IHttpDataAccessConfig>; | ||
nodeConnectionConfig?: Partial<NodeConnectionConfig>; | ||
} = { | ||
httpConfig: {}, | ||
nodeConnectionConfig: {}, | ||
}, | ||
) { | ||
const requestClientVersion = packageJson.version; | ||
this.httpConfig = { | ||
...httpConfigDefaults, | ||
...httpConfig, | ||
}; | ||
this.nodeConnectionConfig = { | ||
baseURL: 'http://localhost:3000', | ||
headers: { | ||
[this.httpConfig.requestClientVersionHeader]: requestClientVersion, | ||
}, | ||
...nodeConnectionConfig, | ||
}; | ||
} | ||
|
||
/** | ||
* Sends an HTTP GET request to the node and retries until it succeeds. | ||
* Throws when the retry count reaches a maximum. | ||
* | ||
* @param url HTTP GET request url | ||
* @param params HTTP GET request parameters | ||
* @param retryConfig Maximum retry count, delay between retries, exponential backoff delay, and maximum exponential backoff delay | ||
*/ | ||
public async fetchAndRetry<T = unknown>( | ||
path: string, | ||
params: Record<string, unknown>, | ||
retryConfig: { | ||
maxRetries?: number; | ||
retryDelay?: number; | ||
exponentialBackoffDelay?: number; | ||
maxExponentialBackoffDelay?: number; | ||
} = {}, | ||
): Promise<T> { | ||
retryConfig.maxRetries = retryConfig.maxRetries ?? this.httpConfig.httpRequestMaxRetry; | ||
retryConfig.retryDelay = retryConfig.retryDelay ?? this.httpConfig.httpRequestRetryDelay; | ||
retryConfig.exponentialBackoffDelay = | ||
retryConfig.exponentialBackoffDelay ?? this.httpConfig.httpRequestExponentialBackoffDelay; | ||
retryConfig.maxExponentialBackoffDelay = | ||
retryConfig.maxExponentialBackoffDelay ?? | ||
this.httpConfig.httpRequestMaxExponentialBackoffDelay; | ||
return await retry(async () => await this.fetch<T>('GET', path, params), retryConfig)(); | ||
} | ||
|
||
public async fetch<T = unknown>( | ||
method: 'GET' | 'POST', | ||
path: string, | ||
params: Record<string, unknown> | undefined, | ||
body?: Record<string, unknown>, | ||
): Promise<T> { | ||
const { baseURL, headers, ...options } = this.nodeConnectionConfig; | ||
const url = new URL(path, baseURL); | ||
if (params) { | ||
// qs.parse doesn't handle well mixes of string and object params | ||
for (const [key, value] of Object.entries(params)) { | ||
if (typeof value === 'object') { | ||
params[key] = JSON.stringify(value); | ||
} | ||
} | ||
url.search = stringify(params); | ||
} | ||
kevindavee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const r = await fetch(url, { | ||
method, | ||
body: body ? JSON.stringify(body) : undefined, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...headers, | ||
}, | ||
...options, | ||
}); | ||
if (r.ok) { | ||
return await r.json(); | ||
} | ||
|
||
throw Object.assign(new Error(r.statusText), { | ||
status: r.status, | ||
statusText: r.statusText, | ||
}); | ||
} | ||
kevindavee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.