diff --git a/types/d3-indirections/d3-indirections-tests.ts b/types/d3-indirections/d3-indirections-tests.ts new file mode 100644 index 0000000000..b185dacad5 --- /dev/null +++ b/types/d3-indirections/d3-indirections-tests.ts @@ -0,0 +1,62 @@ +/// + +function request(url: string, method: string, b: (response: any) => void, body?: any) { + const a = new XMLHttpRequest(); + a.open(method, url); + a.onreadystatechange = function() { + if (this.readyState === 4 && this.status === 200) { + b(JSON.parse(this.responseText)); + } + }; + a.send(body); +} + +request('GET', 'http://localhost:80/api/v1/indirections', (response: AllIndirectionsResponse) => { + response.result.forEach(value => { + console.log(`${value.uid}:: ${value.name} is a ${value.resourceType}`); + }); +}); + +const indirectionUid = '1832822376423'; +request('GET', 'http://localhost:80/api/v1/indirections/' + indirectionUid, (response: IndirectionByUidResponse) => { + const value = response.result; + console.log(`${value.uid}:: ${value.name} is a ${value.resourceType}`); +}); + +const resourceType = 'VideoClip'; +request( + 'GET', + 'http://localhost:80/api/v1/resources?type=' + resourceType, + (response: AllResourcesResponse) => { + response.result.forEach(value => { + console.log(`${value.uid}:: ${value.name} is a ${value.type} stored at ${value.path}`); + }); + }, +); + +const resourceUid = '93264385638475'; +request('GET', 'http://localhost:80/api/v1/resources/' + resourceUid, (response: ResourceByUidResponse) => { + const value = response.result; + console.log(`${value.uid}:: ${value.name} is a ${value.type} stored at ${value.path}`); + + if ((value.type as string) === 'VideoClip') { + console.log('It is definitely a video clip'); + } +}); + +const assignments: Assignments = { + assignments: { + uid: indirectionUid, + resourceUid, + }, +}; +request( + 'PUT', + 'http://localhost:80/api/v1/indirections', + (response: AssignmentsResponse) => { + response.failedAssignments.forEach(value => { + console.log(`Failure with ${value.uid}: ${value.error}`); + }); + }, + assignments, +); diff --git a/types/d3-indirections/index.d.ts b/types/d3-indirections/index.d.ts new file mode 100644 index 0000000000..a56b31472e --- /dev/null +++ b/types/d3-indirections/index.d.ts @@ -0,0 +1,157 @@ +// Type definitions for non-npm package d3-indirections 0.1 +// Project: https://github.com/herobank110/types-d3-indirections +// Definitions by: David Kanekanian +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * Type of resource indirections may control. + */ +type ResourceType = 'VideoClip' | 'Projection' | 'unknown'; + +/** + * Base class for all indirections. Should not be used directly. + * + * Indirections are controlled by an Indirection Controller. When + * an indirection is changed, all indirections controlled by the + * same indirection controller will also be affected. + */ +interface IndirectionBase { + /** Unique identifier for this indirection. */ + uid: string; + + /** The type of resource the indirection controller is associated with. */ + resourceType: ResourceType; + + /** The display name of this indirection. */ + name: string; +} + +/** + * An indirection that can have a resource. + */ +interface ManualIndirection extends IndirectionBase { + manualIndirection: { + /** The resource currently in use. */ + resourceUid: string; + }; +} + +/** + * An indirection with a list of resources to choose from. + */ +interface ListIndirection extends IndirectionBase { + listIndirection: { + /** The index of the resource list currently being used. */ + resourceIndex: number; + + /** List of resources available to pick from. */ + resourceUids: string[]; + }; +} + +/** + * Any type of indirection, received from a GET request. + */ +type Indirection = ManualIndirection | ListIndirection; + +/** + * Base class for assigning values to existing indirections. + */ +interface AssignmentBase { + /** Unique identifier of the indirection. */ + uid: string; +} + +/** + * Assign a resource to a manual indirection. + */ +interface ManualAssignment extends AssignmentBase { + /** Unique identifier of the new resource to use. */ + resourceUid: string; +} + +/** + * Assign a list resource by setting its current index. + */ +interface ListAssignment extends AssignmentBase { + /** New index of the resource list to use. */ + resourceIndex: number; +} + +/** + * Any Indirection Resource assignment. + * + * This abstract type never gets sent via a PUT request. + */ +type Assignment = ManualAssignment | ListAssignment; + +/** + * This is actually what gets sent to the PUT assignment. + */ +interface Assignments { + assignments: Assignment | Assignment[]; +} + +/** + * Base class for all GET responses. Should not be used directly. + */ +interface ResponseBase { + result: any; +} + +/** + * Response from a GET indirection by UID request. + */ +interface IndirectionByUidResponse extends ResponseBase { + result: Indirection; +} + +/** + * Response from a GET all indirections request. + */ +interface AllIndirectionsResponse extends ResponseBase { + result: Indirection[]; +} + +/** + * A resource used by d3, such as a video or audio clip. + */ +interface Resource { + /** Unique identifier for the resource. */ + uid: string; + + /** The name of the resource displayed in d3. */ + name: string; + + /** THe underlying d3 system filepath of the resource. */ + path: string; + + /** The type of resource. */ + type: T; +} + +/** + * Response from a GET resource by UID request. + */ +interface ResourceByUidResponse extends ResponseBase { + result: Resource; +} + +/** + * Response from a GET all resource of type request. + */ +interface AllResourcesResponse extends ResponseBase { + result: Array>; +} + +/** + * The requested assignment is returned with an error property. + */ +type AssignmentFailure = Assignment & { error: string }; + +/** + * Response from a PUT indirections resource request. + */ +interface AssignmentsResponse { + failedAssignments: AssignmentFailure[]; +} diff --git a/types/d3-indirections/tsconfig.json b/types/d3-indirections/tsconfig.json new file mode 100644 index 0000000000..a10e44c15c --- /dev/null +++ b/types/d3-indirections/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "d3-indirections-tests.ts" + ] +} diff --git a/types/d3-indirections/tslint.json b/types/d3-indirections/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/d3-indirections/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }