Add d3-indirections types (#47641)

* Add d3 indirections types

* Fix d3-indirections types lint

* Add non-npm package for types

* Add non npm package types version

* Change types reference to d3-indirections
This commit is contained in:
herobank110 2020-09-17 17:20:13 +01:00 committed by GitHub
parent 0718ea0f9f
commit 22da8b47ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 244 additions and 0 deletions

View File

@ -0,0 +1,62 @@
/// <reference types="d3-indirections" />
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<typeof resourceType>) => {
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,
);

157
types/d3-indirections/index.d.ts vendored Normal file
View File

@ -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 <https://github.com/herobank110>
// 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<T extends ResourceType> {
/** 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<T extends ResourceType = "unknown"> extends ResponseBase {
result: Resource<T>;
}
/**
* Response from a GET all resource of type request.
*/
interface AllResourcesResponse<T extends ResourceType> extends ResponseBase {
result: Array<Resource<T>>;
}
/**
* 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[];
}

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }