Skip to content

Commit

Permalink
Rename KuzzleRequest to RequestPayload (#565)
Browse files Browse the repository at this point in the history
Rename KuzzleRequest and KuzzleResponse to RequestPayload and ResponsePayload

Also move type definitions to types/

Those types are exactly the same as in Kuzzle.
  • Loading branch information
Aschen authored Dec 7, 2020
1 parent 76f3e5b commit 731b99f
Show file tree
Hide file tree
Showing 33 changed files with 565 additions and 470 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ src/protocols/abstract/Realtime.js
src/protocols/Http.js
src/protocols/WebSocket.js
src/protocols/index.js
src/utils/interfaces.js
src/types/*.js
src/core/KuzzleEventEmitter.js
src/core/searchResult/SearchResultBase.js
src/core/searchResult/Document.js
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ src/protocols/abstract/Realtime.js
src/protocols/Http.js
src/protocols/WebSocket.js
src/protocols/index.js
src/utils/interfaces.js
src/types/*.js
src/core/KuzzleEventEmitter.js
src/core/searchResult/SearchResultBase.js
src/core/searchResult/Document.js
Expand Down
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export * from './src/core/searchResult/Role';
export * from './src/core/searchResult/Specifications';
export * from './src/core/searchResult/User';

export * from './src/utils/interfaces';
export * from './src/types';

export * from './src/controllers/Auth';
export * from './src/controllers/Base';
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kuzzle-sdk",
"version": "7.4.3",
"version": "7.4.4",
"description": "Official Javascript SDK for Kuzzle",
"author": "The Kuzzle Team <support@kuzzle.io>",
"repository": {
Expand Down
8 changes: 5 additions & 3 deletions src/Kuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import { MemoryStorageController } from './controllers/MemoryStorage';

import { uuidv4 } from './utils/uuidv4';
import { proxify } from './utils/proxify';
import { JSONObject, KuzzleRequest, KuzzleResponse } from './utils/interfaces';
import { JSONObject } from './types';
import { RequestPayload } from './types/RequestPayload';
import { ResponsePayload } from './types/ResponsePayload';

// Defined by webpack plugin
declare const SDKVERSION: any;
Expand Down Expand Up @@ -132,7 +134,7 @@ export class Kuzzle extends KuzzleEventEmitter {
* Custom function called during offline mode to filter
* queued requests on-the-fly
*/
queueFilter?: (request: KuzzleRequest) => boolean;
queueFilter?: (request: RequestPayload) => boolean;
/**
* Called before dequeuing requests after exiting offline mode,
* to add items at the beginning of the offline queue
Expand Down Expand Up @@ -524,7 +526,7 @@ export class Kuzzle extends KuzzleEventEmitter {
* @param request
* @param options - Optional arguments
*/
query (request: KuzzleRequest = {}, options: JSONObject = {}): Promise<KuzzleResponse> {
query (request: RequestPayload = {}, options: JSONObject = {}): Promise<ResponsePayload> {
if (typeof request !== 'object' || Array.isArray(request)) {
throw new Error(`Kuzzle.query: Invalid request: ${JSON.stringify(request)}`);
}
Expand Down
4 changes: 4 additions & 0 deletions src/KuzzleError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export class KuzzleError extends Error {
});
}

if (stack) {
this.stack = stack;
}

this.id = apiError.id;
this.code = apiError.code;

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Jwt } from '../core/Jwt';
import { BaseController } from './Base';
import { User } from '../core/security/User';
import { JSONObject, ApiKey } from '../utils/interfaces';
import { JSONObject, ApiKey } from '../types';

/**
* Auth controller
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/Base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { KuzzleRequest, JSONObject } from '../utils/interfaces';
import { JSONObject } from '../types';
import { RequestPayload } from '../types/RequestPayload';

export class BaseController {
private _name: string;
Expand Down Expand Up @@ -34,7 +35,7 @@ export class BaseController {
* @param request
* @param options
*/
query (request: KuzzleRequest = {}, options: any = {}): Promise<JSONObject> {
query (request: RequestPayload = {}, options: any = {}): Promise<JSONObject> {
request.controller = request.controller || this.name;

return this._kuzzle.query(request, options);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Collection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseController } from './Base';
import { SpecificationsSearchResult } from '../core/searchResult/Specifications';
import { CollectionMappings, JSONObject } from '../utils/interfaces';
import { CollectionMappings, JSONObject } from '../types';

export class CollectionController extends BaseController {
constructor (kuzzle) {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Document.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BaseController } from './Base';
import { SearchResult } from '../core/searchResult/SearchResultBase';
import { DocumentSearchResult } from '../core/searchResult/Document';
import { JSONObject, Document, DocumentHit } from '../utils/interfaces';
import { JSONObject, Document, DocumentHit } from '../types';

export class DocumentController extends BaseController {
constructor (kuzzle) {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/Realtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BaseController } from './Base';
import Room from '../core/Room';
import { Notification, JSONObject } from '../utils/interfaces';
import { Notification, JSONObject } from '../types';

/**
* Enum for `scope` option of realtime.subscribe method
Expand Down
2 changes: 1 addition & 1 deletion src/core/searchResult/Document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SearchResultBase } from './SearchResultBase';
import { DocumentHit } from '../../utils/interfaces';
import { DocumentHit } from '../../types';

export class DocumentSearchResult extends SearchResultBase<DocumentHit> {
/**
Expand Down
7 changes: 4 additions & 3 deletions src/core/searchResult/SearchResultBase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JSONObject, KuzzleRequest } from '../../utils/interfaces';
import { JSONObject } from '../../types';
import { RequestPayload } from '../../types/RequestPayload';
import { Kuzzle } from '../../Kuzzle';

export interface SearchResult<T> {
Expand Down Expand Up @@ -42,7 +43,7 @@ export class SearchResultBase<T> implements SearchResult<T> {
protected _searchAction: string;
protected _scrollAction: string;
protected _controller: string;
protected _request: KuzzleRequest;
protected _request: RequestPayload;
protected _kuzzle: Kuzzle;
protected _options: JSONObject;
protected _response: JSONObject;
Expand All @@ -57,7 +58,7 @@ export class SearchResultBase<T> implements SearchResult<T> {

constructor (
kuzzle: Kuzzle,
request: KuzzleRequest = {},
request: RequestPayload = {},
options: JSONObject = {},
response: any = {}
) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/searchResult/Specifications.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SearchResultBase } from './SearchResultBase';
import { JSONObject } from '../../utils/interfaces';
import { JSONObject } from '../../types';

export class SpecificationsSearchResult extends SearchResultBase<JSONObject> {

Expand Down
2 changes: 1 addition & 1 deletion src/core/security/Profile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Role } from './Role';
import { ProfilePolicy } from '../../utils/interfaces';
import { ProfilePolicy } from '../../types';

export class Profile {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/security/Role.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RoleRightsDefinition } from '../../utils/interfaces';
import { RoleRightsDefinition } from '../../types';

export class Role {
/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/security/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { JSONObject } from '../../utils/interfaces';
import { JSONObject } from '../../types';
import { Profile } from './Profile';

export class User {
Expand Down
5 changes: 3 additions & 2 deletions src/protocols/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import staticHttpRoutes from './routes.json';
import { KuzzleAbstractProtocol } from './abstract/Base';
import { HttpRoutes, JSONObject, KuzzleRequest } from '../utils/interfaces';
import { HttpRoutes, JSONObject } from '../types';
import { RequestPayload } from '../types/RequestPayload';

/**
* Http protocol used to connect to a Kuzzle server.
Expand Down Expand Up @@ -177,7 +178,7 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
* @param {Object} data
* @returns {Promise<any>}
*/
send (request: KuzzleRequest, options: JSONObject = {}) {
send (request: RequestPayload, options: JSONObject = {}) {
const route = this.routes[request.controller]
&& this.routes[request.controller][request.action];

Expand Down
5 changes: 3 additions & 2 deletions src/protocols/WebSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import { KuzzleError } from '../KuzzleError';
import { BaseProtocolRealtime } from './abstract/Realtime';
import { JSONObject, KuzzleRequest } from '../utils/interfaces';
import { JSONObject } from '../types';
import { RequestPayload } from '../types/RequestPayload';

/**
* WebSocket protocol used to connect to a Kuzzle server.
Expand Down Expand Up @@ -157,7 +158,7 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
*
* @param {Object} payload
*/
send (request: KuzzleRequest) {
send (request: RequestPayload) {
if (this.client && this.client.readyState === this.client.OPEN) {
this.client.send(JSON.stringify(request));
}
Expand Down
5 changes: 3 additions & 2 deletions src/protocols/abstract/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { KuzzleError } from '../../KuzzleError';
import { uuidv4 } from '../../utils/uuidv4';
import { KuzzleEventEmitter } from '../../core/KuzzleEventEmitter';
import { PendingRequest } from './PendingRequest';
import { KuzzleRequest, JSONObject } from '../../utils/interfaces';
import { JSONObject } from '../../types';
import { RequestPayload } from '../../types/RequestPayload';

export abstract class KuzzleAbstractProtocol extends KuzzleEventEmitter {
private _pendingRequests: Map<string, PendingRequest>;
Expand Down Expand Up @@ -96,7 +97,7 @@ export abstract class KuzzleAbstractProtocol extends KuzzleEventEmitter {

abstract connect (): Promise<any>

abstract send (request: KuzzleRequest, options: JSONObject): void
abstract send (request: RequestPayload, options: JSONObject): void

/**
* Called when the client's connection is established
Expand Down
34 changes: 34 additions & 0 deletions src/types/ApiKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* ApiKey
*/
export type ApiKey = {
/**
* ApiKey unique ID
*/
_id: string;
/**
* ApiKey content
*/
_source: {
/**
* User kuid
*/
userId: string;
/**
* Expiration date in Epoch-millis format (-1 if the token never expires)
*/
expiresAt: number;
/**
* Original TTL in ms
*/
ttl: number;
/**
* API key description
*/
description: string;
/**
* Authentication token associated with this API key
*/
token: string;
}
}
72 changes: 72 additions & 0 deletions src/types/Document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { JSONObject } from './JSONObject';

/**
* Kuzzle metadata
* @see https://docs.kuzzle.io/core/2/guides/essentials/document-metadata/
*/
export interface DocumentMetadata {
_kuzzle_info: {
/**
* Kuid of the user who created the document
*/
author: string,
/**
* Creation date in micro-timestamp
*/
createdAt: number,
/**
* Kuid of the user who last updated the document
*/
updater: string | null,
/**
* Update date in micro-timestamp
*/
updatedAt: number | null
}
}

/**
* Represents the `_source` property of the document
*/
export interface DocumentContent extends DocumentMetadata {
[key: string]: JSONObject | any,
}

/**
* Kuzzle document
*
* @property _id
* @property _version
* @property _source
*/
export class Document {
/**
* Document unique ID
*/
_id: string;
/**
* Document Version (generated by Elasticsearch)
*/
_version?: number;
/**
* Document Content
*/
_source: DocumentContent;

[key: string]: JSONObject | any;
}

/**
* Document retrieved from a search
*
* @property _id
* @property _version
* @property _source
* @property _score
*/
export interface DocumentHit extends Document {
/**
* Relevance score
*/
_score: number;
}
36 changes: 36 additions & 0 deletions src/types/HttpRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* HTTP routes definition format
* @example
* {
* <controller>: {
* <action>: { verb: <verb>, url: <url> }
* }
* }
*
* {
* 'my-plugin/my-controller': {
* action: { verb: 'GET', url: '/some/url' },
* action2: { verb: 'GET', url: '/some/url/with/:parameter' }
* }
* }
*/
export type HttpRoutes = {
/**
* Controller name
*/
[controller: string]: {
/**
* Action name
*/
[action: string]: {
/**
* HTTP verb
*/
verb: string,
/**
* URL
*/
url: string
}
}
}
Loading

0 comments on commit 731b99f

Please sign in to comment.