diff --git a/lib/types/identity-search/auth-message-types.js b/lib/types/identity-search/auth-message-types.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/auth-message-types.js @@ -0,0 +1,21 @@ +// @flow + +import type { TInterface } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type AuthMessage = { + +type: 'AuthMessage', + +userID: string, + +deviceID: string, + +accessToken: string, +}; + +export const authMessageValidator: TInterface = + tShape({ + type: tString('AuthMessage'), + userID: t.String, + deviceID: t.String, + accessToken: t.String, + }); diff --git a/lib/types/identity-search/messages.js b/lib/types/identity-search/messages.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/messages.js @@ -0,0 +1,71 @@ +// @flow + +import type { TUnion } from 'tcomb'; +import t from 'tcomb'; + +import { + type AuthMessage, + authMessageValidator, +} from './auth-message-types.js'; +import { + type SearchQuery, + searchQueryValidator, +} from './search-query-types.js'; +import { + type SearchResponse, + searchResponseValidator, +} from './search-response-types.js'; +import { + type ConnectionInitializationResponse, + connectionInitializationResponseValidator, +} from '../websocket/connection-initialization-response-types.js'; +import { + type Heartbeat, + heartbeatValidator, +} from '../websocket/heartbeat-types.js'; + +/* + * This file defines types and validation for messages sent + * to Identity Search WebSocket server and messages sent to client. + * The definitions in this file should remain in sync + * with the structures defined in the corresponding + * Rust file at `shared/identity_search_messages/src/messages/mod.rs`. + * + * If you edit the definitions in one file, + * please make sure to update the corresponding definitions in the other. + * + */ +export const identitySearchMessageToClientTypes = Object.freeze({ + CONNECTION_INITIALIZATION_RESPONSE: 'ConnectionInitializationResponse', + SUCCESS: 'Success', + ERROR: 'Error', + SEARCH_RESPONSE: 'SearchResponse', + HEARTBEAT: 'Heartbeat', +}); + +export const identitySearchMessageToClientValidator: TUnion = + t.union([ + connectionInitializationResponseValidator, + searchResponseValidator, + heartbeatValidator, + ]); + +export type IdentitySearchMessageToClient = + | ConnectionInitializationResponse + | SearchResponse + | Heartbeat; + +export const identitySearchMessageToServerTypes = Object.freeze({ + AUTH_MESSAGE: 'AuthMessage', + SEARCH_QUERY: 'SearchQuery', + PREFIX: 'Prefix', + HEARTBEAT: 'Heartbeat', +}); + +export const identitySearchMessageToServerValidator: TUnion = + t.union([authMessageValidator, searchQueryValidator, heartbeatValidator]); + +export type IdentitySearchMessageToServer = + | AuthMessage + | SearchQuery + | Heartbeat; diff --git a/lib/types/identity-search/search-query-types.js b/lib/types/identity-search/search-query-types.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/search-query-types.js @@ -0,0 +1,33 @@ +// @flow + +import type { TInterface } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type SearchMethod = Prefix; + +export type Prefix = { + +type: 'Prefix', + +prefix: string, +}; + +export const prefixValidator: TInterface = tShape({ + type: tString('Prefix'), + prefix: t.String, +}); + +export const searchMethodValidator: TInterface = prefixValidator; + +export type SearchQuery = { + +type: 'SearchQuery', + +id: string, + +searchMethod: SearchMethod, +}; + +export const searchQueryValidator: TInterface = + tShape({ + type: tString('SearchQuery'), + id: t.String, + searchMethod: searchMethodValidator, + }); diff --git a/lib/types/identity-search/search-response-types.js b/lib/types/identity-search/search-response-types.js new file mode 100644 --- /dev/null +++ b/lib/types/identity-search/search-response-types.js @@ -0,0 +1,53 @@ +// @flow + +import type { TInterface, TUnion } from 'tcomb'; +import t from 'tcomb'; + +import { tShape, tString } from '../../utils/validation-utils.js'; + +export type Failure = { + +id: string, + +error: string, +}; + +export const failureValidator: TInterface = tShape({ + id: t.String, + error: t.String, +}); + +export type User = { + +userID: string, + +username: string, +}; + +export const userValidator: TInterface = tShape({ + userID: t.String, + username: t.String, +}); + +export type SearchResult = { + +id: string, + +hits: $ReadOnlyArray, +}; + +export const searchResultValidator: TInterface = + tShape({ + id: t.String, + hits: t.list(userValidator), + }); + +type SearchResponseSuccess = { +type: 'Success', +data: SearchResult }; +type SearchResponseError = { +type: 'Error', +data: Failure }; + +export type SearchResponse = SearchResponseSuccess | SearchResponseError; + +export const searchResponseValidator: TUnion = t.union([ + tShape({ + type: tString('Success'), + data: searchResultValidator, + }), + tShape({ + type: tString('Error'), + data: failureValidator, + }), +]);