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

Commit

Permalink
reorganize files and use Pascal or camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenmeier committed Feb 3, 2024
1 parent 238f2c4 commit 9fe150d
Show file tree
Hide file tree
Showing 26 changed files with 227 additions and 264 deletions.
23 changes: 22 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,28 @@
},
"license": "MIT or Apache-2.0",
"exports": {
"./*": "./src/*"
"./api/Api": "./src/api/Api.js",
"./api/ApiError": "./src/api/ApiError.js",
"./api/Stream": "./src/api/Stream.js",
"./collections/SlotArray": "./src/collections/SlotArray.js",
"./data/Data": "./src/data/Data.js",
"./data/MapType": "./src/data/MapType.js",
"./data/ParseType": "./src/data/ParseType.js",
"./data/parseTypes": "./src/data/parseTypes.js",
"./dom/Context2d": "./src/dom/Context2d.js",
"./dom/utils": "./src/dom/utils.js",
"./graphQl/GraphQl": "./src/graphQl/GraphQl.js",
"./graphQl/GraphQlError": "./src/graphQl/GraphQlError.js",
"./sync/Barrier": "./src/sync/Barrier.js",
"./sync/Listeners": "./src/sync/Listeners.js",
"./sync/NonConcurrent": "./src/sync/NonConcurrent.js",
"./time/Date": "./src/time/Date.js",
"./time/DateTime": "./src/time/DateTime.js",
"./time/DateTimeRange": "./src/time/DateTimeRange.js",
"./time/Duration": "./src/time/Duration.js",
"./time/localization": "./src/time/localization.js",
"./utils": "./src/utils/utils.js",
"./utils/colors": "./src/utils/colors.js"
},
"devDependencies": {
"eslint": "^8.45.0",
Expand Down
35 changes: 34 additions & 1 deletion src/api/api.js → src/api/Api.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import ApiError from './error.js';
import ApiError from './ApiError.js';

/**
* Api class to handle requests to a server
*/
export default class Api {
constructor(addr = null) {
this.addr = addr;
}

/**
* Prepares a json object to be sent as a header
*/
static jsonHeader(data) {
return encodeURIComponent(JSON.stringify(data));
}

/**
* 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
*/
async request(method, path, data = null, headers = {}, opts = {}) {
let err;

Expand Down Expand Up @@ -44,6 +59,15 @@ export default class Api {
throw err;
}

/**
* 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
*/
async requestWithFile(method, path, file, progress = null, headers = {}) {
if (!progress) progress = () => {};

Expand Down Expand Up @@ -99,6 +123,15 @@ export default class Api {
});
}

/**
* 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
*/
async requestTimeout(method, path, data = null, headers = {}, timeout = 0) {
if (!this.addr) throw ApiError.newOther('Server addr not defined');

Expand Down
32 changes: 30 additions & 2 deletions src/api/error.js → src/api/ApiError.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
/**
* An error returned from the API
*/
export default class ApiError {
/*
fields:
- kind: str,
- data: any // if it has a toString (it will be stored it in the msg)
*/
/**
* Creates a new ApiError
*
* @param {string} kind - The kind of the error should be in SCREAMING-KEBAB-CASE
* @param {any} data - The data of the error if it provides a toString method it will be stored in the msg
*/
constructor(kind, data) {
this.kind = kind;
this.data = data;
}

/**
* The message of the error
*
* @returns {string}
*/
get msg() {
if (this.data && typeof this.data.toString === 'function')
return this.data.toString();
Expand All @@ -32,19 +46,33 @@ export default class ApiError {
return new ApiError(kind, data);
}

/**
* Creates a new ApiError with the kind 'OTHER'
*/
static newOther(msg) {
return new ApiError('Other', msg);
return new ApiError('OTHER', msg);
}

/**
* Creates a new ApiError with the kind 'SESSION_NOT_FOUND'
*/
static newSessionError() {
return new ApiError('SessionNotFound', 'no session');
return new ApiError('SESSION_NOT_FOUND', 'no session');
}

/**
* Returns a string representation of the error
*/
toString() {
return `${this.kind}: ${this.msg}`;
}
}

/**
* Returns whether the value is an ApiError object
*
* @returns {boolean}
*/
export function isApiErrorObject(val) {
return typeof (val ? val.__isApiErrorObject__ : null) === 'function';
}
47 changes: 24 additions & 23 deletions src/api/stream.js → src/api/Stream.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import ApiError from './error.js';
import Data from './../data/data.js';
import Listeners from './../util/listeners.js';

/// The Stream is responsible for managing your connection with a server
/// when you call connect Stream will try to create a WebSocket connection.
///
/// At this point you can start a Sender or Receiver. The request will wait
/// until a channel could be established or the server closed it.
///
/// You can listen for messages or send them until you receive the error event
/// which will mean the channel was closed. You can then try to create a new
/// one.
///
/// When the Stream triggers an error event this might mean the connection is
/// closed but doesn't have to. You should wait until you receive the close
/// event to be sure. After that you can call connect again.
///
///
/// ## Important
/// Only use functions all properties are private also functions prefix with an
/// underscore.
///
import ApiError from './ApiError.js';
import Data from '../data/Data.js';
import Listeners from '../sync/Listeners.js';

/**
* The Stream is responsible for managing your connection with a server
* when you call connect Stream will try to create a WebSocket connection.
*
* At this point you can start a Sender or Receiver. The request will wait
* until a channel could be established or the server closed it.
*
* You can listen for messages or send them until you receive the error event
* which will mean the channel was closed. You can then try to create a new
* one.
*
* When the Stream triggers an error event this might mean the connection is
* closed but doesn't have to. You should wait until you receive the close
* event to be sure. After that you can call connect again.
*
*
* ## Important
* Only use functions all properties are private also functions prefix with an
* underscore.
*/
// ## Internal
// We expect that the WebSocket class only triggers a close event if the connect
// was established. This is the case in some circumstances but not in all.
Expand Down
2 changes: 1 addition & 1 deletion src/util/slotarray.js → src/collections/SlotArray.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Implements a slot array data structure.
* @class
* @exports util/slotarray/SlotArray
* @exports collections/SlotArray/SlotArray
*/
export default class SlotArray {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/data/data.js → src/data/Data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseType, parseTypeDefault } from './parsetype.js';
import { parseType, parseTypeDefault } from './parseTypes.js';

export default class Data {
// the data will be assigned to this
Expand Down
4 changes: 2 additions & 2 deletions src/data/map.js → src/data/MapType.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseType } from './parsetype.js';
import ParseType from './parsetypes.js';
import ParseType from './ParseType.js';
import { parseType } from './parseTypes.js';

export default class MapType extends ParseType {
constructor(keyType, valueType) {
Expand Down
20 changes: 20 additions & 0 deletions src/data/OptionType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import ParseType from './ParseType.js';
import { parseType } from './parseTypes.js';

export class Option extends ParseType {
constructor(innerType) {
super();
this.innerType = innerType;
}

parse(val) {
if (typeof val === 'undefined' || val === null) return null;
return parseType(this.innerType, val);
}

default() {
return null;
}

__allowedUndefined__() {}
}
11 changes: 11 additions & 0 deletions src/data/ParseType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default class ParseType {
static __parsetype__() {}
static parse(_data) {
throw new Error('static parse not implemented');
}

__parsetype__() {}
parse(_data) {
throw new Error('parse not implemented');
}
}
71 changes: 70 additions & 1 deletion src/data/parsetype.js → src/data/parseTypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
import { typeFromStr } from './parsetypes.js';
import ParseType from './ParseType.js';
import OptionType from './OptionType.js';
import DateTime from '../time/DateTime.js';

export class StringType extends ParseType {
static parse(val) {
if (typeof val !== 'string') throw new Error('expected a string');

return val;
}
}

export class BoolType extends ParseType {
static parse(val) {
return !!val;
}
}

export class IntType extends ParseType {
static parse(val) {
return parseInt(val);
}
}

export class FloatType extends ParseType {
static parse(val) {
return parseFloat(val);
}
}

export class AnyType extends ParseType {
static parse(val) {
return val;
}
}

export class UniqueIdType extends ParseType {
static parse(val) {
if (typeof val !== 'string' || val.length !== 14)
throw new Error('expected uid');
return val;
}
}

export function typeFromStr(type) {
// allow for optstr
if (type.length > 3 && type.slice(0, 3) === 'opt')
return new OptionType(typeFromStr(type.slice(3)));

switch (type) {
case 'arr':
throw new Error('use [] instead');
case 'uid':
return UniqueIdType;
case 'str':
return StringType;
case 'bool':
return BoolType;
case 'int':
return IntType;
case 'float':
return FloatType;
case 'datetime':
return DateTime;
case 'any':
return AnyType;
default:
throw new Error(`unrecognized type ${type}`);
}
}

export function parseType(type, val) {
switch (typeof type) {
Expand Down
Loading

0 comments on commit 9fe150d

Please sign in to comment.