refactor: support inrupt coding standards

This commit is contained in:
Joachim Van Herwegen
2020-05-20 17:27:54 +02:00
parent 5def53180c
commit f8e136cadb
25 changed files with 722 additions and 604 deletions

View File

@@ -0,0 +1,6 @@
import { ResourceStore } from './ResourceStore';
/**
* A {@link ResourceStore} of which all operations are atomic.
*/
export interface AtomicResourceStore extends ResourceStore {}

35
src/storage/Conditions.ts Normal file
View File

@@ -0,0 +1,35 @@
import { RepresentationMetadata } from '../ldp/http/RepresentationMetadata';
/**
* The conditions of an HTTP conditional request.
*/
export interface Conditions {
/**
* Valid if matching any of the given ETags.
*/
matchesEtag: string[];
/**
* Valid if not matching any of the given ETags.
*/
notMatchesEtag: string[];
/**
* Valid if modified since the given date.
*/
modifiedSince?: Date;
/**
* Valid if not modified since the given date.
*/
unmodifiedSince?: Date;
/**
* Checks validity based on the given metadata.
* @param metadata - Metadata of the representation.
*/
matchesMetadata: (metadata: RepresentationMetadata) => boolean;
/**
* Checks validity based on the given ETag and/org date.
* @param eTag - Condition based on ETag.
* @param lastModified - Condition based on last modified date.
*/
matches: (eTag?: string, lastModified?: Date) => boolean;
}

10
src/storage/Lock.ts Normal file
View File

@@ -0,0 +1,10 @@
/**
* Lock used by a {@link ResourceLocker} for non-atomic operations.
*/
export interface Lock {
/**
* Release this lock.
* @returns A promise resolving when the release is finished.
*/
release: () => Promise<void>;
}

View File

@@ -0,0 +1,24 @@
import { Representation } from '../ldp/http/Representation';
import { RepresentationPreferences } from '../ldp/http/RepresentationPreferences';
/**
* Allows converting from one resource representation to another.
*/
export interface RepresentationConverter {
/**
* Checks if the converter supports converting the given resource based on the given preferences.
* @param representation - The input representation.
* @param preferences - The requested representation preferences.
*
* @returns A promise resolving to a boolean representing whether this conversion can be done.
*/
supports: (representation: Representation, preferences: RepresentationPreferences) => Promise<boolean>;
/**
* Converts the given representation.
* @param representation - The input representation to convert.
* @param preferences - The requested representation preferences.
*
* @returns A promise resolving to the requested representation.
*/
convert: (representation: Representation, preferences: RepresentationPreferences) => Promise<Representation>;
}

View File

@@ -0,0 +1,15 @@
import { Lock } from './Lock';
import { ResourceIdentifier } from '../ldp/http/ResourceIdentifier';
/**
* Allows the locking of resources which is needed for non-atomic {@link ResourceStore}s.
*/
export interface ResourceLocker {
/**
* Lock the given resource.
* @param identifier - Identifier of the resource that needs to be locked.
*
* @returns A promise containing the lock on the resource.
*/
acquire: (identifier: ResourceIdentifier) => Promise<Lock>;
}

View File

@@ -0,0 +1,22 @@
import { RepresentationMetadata } from '../ldp/http/RepresentationMetadata';
/**
* Supports mapping a file to an URL and back.
*/
export interface ResourceMapper {
/**
* Maps the given file to an URL.
* @param file - The input file.
*
* @returns A promise resolving to the corresponding URL and metadata of the representation.
*/
mapFilePathToUrl: (file: File) => Promise<{ url: URL; metadata: RepresentationMetadata }>;
/**
* Maps the given URL and metadata to a file.
* @param url - The input URL.
* @param metadata - The representation metadata.
*
* @returns A promise resolving to the corresponding file.
*/
mapUrlToFilePath: (url: URL, metadata: RepresentationMetadata) => Promise<File>;
}

View File

@@ -0,0 +1,76 @@
import { Conditions } from './Conditions';
import { Patch } from '../ldp/http/Patch';
import { Representation } from '../ldp/http/Representation';
import { RepresentationPreferences } from '../ldp/http/RepresentationPreferences';
import { ResourceIdentifier } from '../ldp/http/ResourceIdentifier';
/**
* A ResourceStore represents a collection of resources.
* It has been designed such that each of its methods
* can be implemented in an atomic way: for each CRUD operation, only one
* dedicated method needs to be called. A fifth method enables the optimization
* of partial updates with PATCH. It is up to the implementer of the interface to
* (not) make an implementation atomic.
*/
export interface ResourceStore {
/**
* Read a resource.
* @param identifier - Identifier of the resource to read.
* @param preferences - Representation preferences.
* @param conditions - Optional conditions.
*
* @returns A promise containing the representation.
*/
getRepresentation: (
identifier: ResourceIdentifier,
preferences: RepresentationPreferences,
conditions?: Conditions,
) => Promise<Representation>;
/**
* Create a resource.
* @param container - Container in which to create a resource.
* @param representation - Representation of the new resource
* @param conditions - Optional conditions.
*
* @returns A promise containing the new identifier.
*/
addResource: (
container: ResourceIdentifier,
representation: Representation,
conditions?: Conditions,
) => Promise<ResourceIdentifier>;
/**
* Fully update a resource.
* @param identifier - Identifier of resource to update.
* @param representation - New representation of the resource.
* @param conditions - Optional conditions.
*
* @returns A promise resolving when the update is finished.
*/
setRepresentation: (
identifier: ResourceIdentifier,
representation: Representation,
conditions?: Conditions,
) => Promise<void>;
/**
* Delete a resource.
* @param identifier - Identifier of resource to delete.
* @param conditions - Optional conditions.
*
* @returns A promise resolving when the delete is finished.
*/
deleteResource: (identifier: ResourceIdentifier, conditions?: Conditions) => Promise<void>;
/**
* Partially update a resource.
* @param identifier - Identifier of resource to update.
* @param patch - Description of which parts to update.
* @param conditions - Optional conditions.
*
* @returns A promise resolving when the update is finished.
*/
modifyResource: (identifier: ResourceIdentifier, patch: Patch, conditions?: Conditions) => Promise<void>;
}