Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
add more types
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmeier committed Feb 5, 2024
1 parent 954171f commit 09be097
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"editor.formatOnPaste": true,
"editor.formatOnSave": true
}
78 changes: 78 additions & 0 deletions src/api/Api.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import ApiError from './ApiError';

/**
* Api class to handle requests to a server
*/
export default class Api {
/**
* get or set the api address
*/
addr: string;

/**
* Creates a new Api instance
*/
constructor(addr?: string);

/**
* Prepares a json object to be sent as a header
*/
static jsonHeader(data: object): string;

/**
* Send a request to the server
*
* @param {string} method - The method of the request
* @param {string} path - The path of the request
* @param {object|null} data - The data to be sent
* @param {object} headers - The headers to be sent
* @param {object} opts - The additional options to be sent to fetch
*
* @returns The response of the request
*
* @throws {ApiError} - If the request fails
*/
request(
method: string,
path: string,
data?: object | null,
headers?: object,
opts?: object,
): Promise<object>;

/**
* Send a request to the server with a file
*
* @param {string} method - The method of the request
* @param {string} path - The path of the request
* @param {File} file - The file to be sent
* @param {function|null} progress - The progress callback
* @param {object} headers - The headers to be sent
*
* @throws {ApiError} - If the request fails
*/
requestWithFile(
method: string,
path: string,
file: File,
progress: ((event: ProgressEvent) => void) | null,
headers: object,
): Promise<object>;

/**
* Send a request to the server with a timeout
*
* @param {string} method - The method of the request
* @param {string} path - The path of the request
* @param {object|null} data - The data to be sent
* @param {object} headers - The headers to be sent
* @param {number} timeout - The timeout of the request if the value is 0 there is no timeout
*/
requestTimeout(
method: string,
path: string,
data?: object | null,
headers?: object,
timeout?: number,
): Promise<object>;
}
47 changes: 47 additions & 0 deletions src/api/ApiError.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export default class ApiError {
/**
* The kind of error
*/
kind: string;

/**
* The data associated with this error
*
* should provide a toString method
*/
data: any;

/**
* Creates a new ApiError
*/
constructor(kind: string, data: any);

/**
* The message of the error
*
* @returns {string}
*/
get msg(): string;

/**
* Creates a new ApiError with the kind 'OTHER'
*/
static newOther(data: any): ApiError;

/**
* Creates a new ApiError with the kind 'SESSION_NOT_FOUND'
*/
static newSessionError(): ApiError;

/**
* Returns a string representation of the error
*/
toString(): string;
}

/**
* Returns whether the value is an ApiError object
*
* @returns {boolean}
*/
export function isApiErrorObject(value: any): boolean;
17 changes: 17 additions & 0 deletions src/sync/NonConcurrent.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export type NonConcurrentReady = {
ready: () => void;
};

export default class NonConcurrent {
/**
* Creates a new NonConcurrent
*/
constructor();

/**
* Waits until any other non-concurrent requests is done then waits until you call ready
*
* returns an object where you need to call ready once done
*/
start(): Promise<NonConcurrentReady>;
}
9 changes: 8 additions & 1 deletion src/sync/NonConcurrent.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
/// synchronisation point
export default class NonConcurrent {
/**
* Creates a new NonConcurrent
*/
constructor() {
this.listeners = [];
this.running = false;
}

// returns an object where you need to call ready once done
/**
* Waits until any other non-concurrent requests is done then waits until you call ready
*
* returns an object where you need to call ready once done
*/
async start() {
if (this.running) {
await new Promise(resolve => {
Expand Down
116 changes: 116 additions & 0 deletions src/time/Date.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
export default class Date {
/**
* Tries to creates a new Date instance
*/
static parse(val: any): Date;

/**
* Create a new Date instance
* @constructor
* @param {Date|string|number} date - The date value. Default is
* current date.
* If it's a string or number, convert it to a date.
* @throws {Error} Will throw an error if date is an invalid Date.
*/
constructor(date?: Date | string | number);

/**
* Check if the date represented by this instance is today
* @returns {boolean} True if the date represented by this instance is
* today, otherwise false
*/
isToday(): boolean;

/**
* Get the year from the date ex: 2023
* @returns {number} The year in which the date occurs
*/
get year(): number;

/**
* Get the month from the date (0 indexed)
* @returns {number} The month in which the date occurs, zero-indexed
*/
get month(): number;

/**
* Get the date (day of the month 1-31)
* @returns {number} The day of the month on which the date occurs
*/
get date(): number;

/**
* Get the week of the year
* @returns {number} The week of the year in which the date occurs
*/
get week(): number;

/**
* Get the day of the week (0 indexed)
* @returns {number} The day of the week on which the date occurs,
* zero-indexed with 0 for Sunday
*/
get day(): number;

/**
* Get the day of the week (1 indexed with Monday as 1)
* @returns {number} The day of the week on which the date occurs, with 1
* for Monday and 7 for Sunday
*/
get dayMoToSu(): number;

/**
* Get the number of milliseconds since 1 January 1970 00:00:00 UTC
* @returns {number} The number of milliseconds since the Unix Epoch
*/
get time(): number;

/**
* Create a new Date object representing the same day
* @returns {Date} A new Date object with the same year, month, and
* date
*/
clone(): Date;

/**
* Get the name of the month in a given language
* @param {string|null} lang - The language to get the month name in
* @returns {string|null} The name of the month in the given language,
* or null if the language is not provided
*/
toStrMonth(lang?: string | null): string | null;

/**
* Get the name of the day in a given language
* @param {string|null} lang - The language to get the day name in
* @returns {string|null} The name of the day in the given language,
* or null if the language is not provided
*/
toStrDay(lang?: string | null): string | null;

/**
* Get the first letter of the day in a given language, in uppercase
* @param {string|null} lang - The language to get the day name in
* @returns {string|null} The first letter of the day in the given
* language, or null if the language is not provided
*/
toStrDayLetter(lang?: string | null): string | null;

/**
* Get a short representation of the date (dd.mm)
* @returns {string} A string representing the date in the form dd.mm
*/
toStrShort(): string;

/**
* Get a representation of the date (dd.mm.yyyy)
* @returns {string} A string representing the date in the form dd.mm.yyyy
*/
toStr(): string;

/**
* Get a representation of the date suitable for a browser (yyyy-mm-dd)
* @returns {string} A string representing the date in the form yyyy-mm-dd
*/
toBrowserDate(): string;
}
Loading

0 comments on commit 09be097

Please sign in to comment.