Skip to content

Commit

Permalink
Store/Import Typing Updates (#388)
Browse files Browse the repository at this point in the history
* optional target for send

* guard store options
  • Loading branch information
LiranCohen authored Jan 30, 2024
1 parent ae5ec5e commit f24300b
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions packages/api/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,7 @@ export class Record implements RecordModel {
return this._agent.processDwnRequest(request);
}


async store(options: any = {}): Promise<any> {
async _processRecord(options: { store: boolean, import: boolean }): Promise<any> {

Check warning on line 380 in packages/api/src/record.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: ae-undocumented

Missing documentation for "_processRecord".

const { store = true, import: _import = false } = options;

Expand Down Expand Up @@ -432,35 +431,40 @@ export class Record implements RecordModel {

}

async import(options?: any): Promise<any> {
async store(options?: { import: boolean }): Promise<any> {

Check warning on line 434 in packages/api/src/record.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: ae-undocumented

Missing documentation for "store".
return this._processRecord({ ...options, store: true });
}

async import(options?: { store: boolean }): Promise<any> {

Check warning on line 438 in packages/api/src/record.ts

View workflow job for this annotation

GitHub Actions / tbdocs-reporter

extractor: ae-undocumented

Missing documentation for "import".
// Add check to bail if already imported
return this.store({ store: options?.store !== false, import: true });
return this._processRecord({ store: options?.store !== false, import: true });
}


Check failure on line 443 in packages/api/src/record.ts

View workflow job for this annotation

GitHub Actions / test-with-node

Trailing spaces not allowed

/**
* Send the current record to a remote DWN by specifying their DID
* If no DID is specified, the target is assumed to be the owner (connectedDID).
* (vs waiting for the regular DWN sync)
* @param target - the DID to send the record to
* @param target - the optional DID to send the record to, if none is set it is sent to the connectedDid
* @returns the status of the send record request
* @throws `Error` if the record has already been deleted.
*
* @beta
*/
async send(_options?: string | { [key: string]: any }): Promise<ResponseStatus> {
async send(target?: string): Promise<ResponseStatus> {

const initialWrite = this._initialWrite;
const options = !_options ? { target: this._connectedDid } : typeof _options === 'string' ? { target: _options } : _options;

if (!options.target){
options.target = this._connectedDid;
if (!target) {
target = this._connectedDid;
}

if (initialWrite && !Record.checkSendCache(this._recordId, options.target)){
if (initialWrite && !Record.checkSendCache(this._recordId, target)){

const initialState = {
messageType : DwnInterfaceName.Records + DwnMethodName.Write,
author : this._connectedDid,
target : options.target,
target : target,
rawMessage : removeUndefinedProperties({
contextId: this._contextId,
...initialWrite
Expand All @@ -469,15 +473,15 @@ export class Record implements RecordModel {

await this._agent.sendDwnRequest(initialState);

Record.setSendCache(this._recordId, options.target);
Record.setSendCache(this._recordId, target);

}

const latestState = {
messageType : DwnInterfaceName.Records + DwnMethodName.Write,
author : this._connectedDid,
dataStream : await this.data.blob(),
target : options.target
target : target
} as any;

if (this._authorization) {
Expand Down

0 comments on commit f24300b

Please sign in to comment.