|
|
@ -7,34 +7,79 @@ import { |
|
|
|
import { getDataset, type LdoBase, type ShapeType } from "@ldo/ldo"; |
|
|
|
import { getDataset, type LdoBase, type ShapeType } from "@ldo/ldo"; |
|
|
|
import type { SubjectNode } from "@ldo/rdf-utils"; |
|
|
|
import type { SubjectNode } from "@ldo/rdf-utils"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* The methods returned by useLdo |
|
|
|
|
|
|
|
*/ |
|
|
|
export interface UseLdoMethods<Plugins extends ConnectedPlugin[]> { |
|
|
|
export interface UseLdoMethods<Plugins extends ConnectedPlugin[]> { |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* A ConnectedLdoDataset |
|
|
|
|
|
|
|
*/ |
|
|
|
dataset: ConnectedLdoDataset<Plugins>; |
|
|
|
dataset: ConnectedLdoDataset<Plugins>; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Retireves a representation of a Resource at the given URI. This resource |
|
|
|
|
|
|
|
* represents the current state of the resource: whether it is currently |
|
|
|
|
|
|
|
* fetched or in the process of fetching as well as some information about it. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param uri - the URI of the resource |
|
|
|
|
|
|
|
* @param pluginName - optionally, force this function to choose a specific |
|
|
|
|
|
|
|
* plugin to use rather than perform content negotiation. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @returns a Resource |
|
|
|
|
|
|
|
*/ |
|
|
|
getResource: ConnectedLdoDataset<Plugins>["getResource"]; |
|
|
|
getResource: ConnectedLdoDataset<Plugins>["getResource"]; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sets conetext for a specific plugin |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param pluginName - the name of the plugin |
|
|
|
|
|
|
|
* @param context - the context for this specific plugin |
|
|
|
|
|
|
|
*/ |
|
|
|
setContext: ConnectedLdoDataset<Plugins>["setContext"]; |
|
|
|
setContext: ConnectedLdoDataset<Plugins>["setContext"]; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Gets a linked data object based on the subject |
|
|
|
|
|
|
|
*/ |
|
|
|
getSubject<Type extends LdoBase>( |
|
|
|
getSubject<Type extends LdoBase>( |
|
|
|
shapeType: ShapeType<Type>, |
|
|
|
shapeType: ShapeType<Type>, |
|
|
|
subject: string | SubjectNode, |
|
|
|
subject: string | SubjectNode, |
|
|
|
): Type; |
|
|
|
): Type; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Shorthand for connectedLdoDataset |
|
|
|
|
|
|
|
* .usingType(shapeType) |
|
|
|
|
|
|
|
* .write(...resources.map((r) => r.uri)) |
|
|
|
|
|
|
|
* .fromSubject(subject); |
|
|
|
|
|
|
|
* @param shapeType - The shapetype to represent the data |
|
|
|
|
|
|
|
* @param subject - A subject URI |
|
|
|
|
|
|
|
* @param resources - The resources changes to should written to |
|
|
|
|
|
|
|
*/ |
|
|
|
createData<Type extends LdoBase>( |
|
|
|
createData<Type extends LdoBase>( |
|
|
|
shapeType: ShapeType<Type>, |
|
|
|
shapeType: ShapeType<Type>, |
|
|
|
subject: string | SubjectNode, |
|
|
|
subject: string | SubjectNode, |
|
|
|
resource: Plugins[number]["types"]["resource"], |
|
|
|
resource: Plugins[number]["types"]["resource"], |
|
|
|
...additionalResources: Plugins[number]["types"]["resource"][] |
|
|
|
...additionalResources: Plugins[number]["types"]["resource"][] |
|
|
|
): Type; |
|
|
|
): Type; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Returns a writable LinkedDataObject given a linked data object |
|
|
|
|
|
|
|
*/ |
|
|
|
changeData<Type extends LdoBase>( |
|
|
|
changeData<Type extends LdoBase>( |
|
|
|
input: Type, |
|
|
|
input: Type, |
|
|
|
resource: Plugins[number]["types"]["resource"], |
|
|
|
resource: Plugins[number]["types"]["resource"], |
|
|
|
...additionalResources: Plugins[number]["types"]["resource"][] |
|
|
|
...additionalResources: Plugins[number]["types"]["resource"][] |
|
|
|
): Type; |
|
|
|
): Type; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Commits the data of a writable Linke Data Object back to the remote. |
|
|
|
|
|
|
|
*/ |
|
|
|
commitData( |
|
|
|
commitData( |
|
|
|
input: LdoBase, |
|
|
|
input: LdoBase, |
|
|
|
): ReturnType<ConnectedLdoTransactionDataset<Plugins>["commitToRemote"]>; |
|
|
|
): ReturnType<ConnectedLdoTransactionDataset<Plugins>["commitToRemote"]>; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* @internal |
|
|
|
|
|
|
|
* Creates the useLdoHook |
|
|
|
|
|
|
|
*/ |
|
|
|
export function createUseLdo<Plugins extends ConnectedPlugin[]>( |
|
|
|
export function createUseLdo<Plugins extends ConnectedPlugin[]>( |
|
|
|
dataset: ConnectedLdoDataset<Plugins>, |
|
|
|
dataset: ConnectedLdoDataset<Plugins>, |
|
|
|
) { |
|
|
|
) { |
|
|
|
return (): UseLdoMethods<Plugins> => ({ |
|
|
|
const toReturn = { |
|
|
|
dataset, |
|
|
|
dataset, |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Gets a resource |
|
|
|
* Gets a resource |
|
|
@ -94,5 +139,8 @@ export function createUseLdo<Plugins extends ConnectedPlugin[]>( |
|
|
|
) as ConnectedLdoTransactionDataset<Plugins>; |
|
|
|
) as ConnectedLdoTransactionDataset<Plugins>; |
|
|
|
return inputDataset.commitToRemote(); |
|
|
|
return inputDataset.commitToRemote(); |
|
|
|
}, |
|
|
|
}, |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
return function useLdo(): UseLdoMethods<Plugins> { |
|
|
|
|
|
|
|
return toReturn; |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|