@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
250 lines • 7.55 kB
JavaScript
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
import { ContentType } from '../client/constants/content-type';
import { Header } from '../client/constants/header';
/**
* Option provider for options. Options will be replaced if methods are called multiple times.
*/
export class AbstractRequestOptions {
constructor() {
this.options = new Map();
this.headers = new Map();
}
getOptions() {
return new Map(this.options);
}
getHeaders() {
return new Map(this.headers);
}
addRequestParameter(id, value) {
this.options.set(id, value);
return this;
}
addHeader(name, value) {
this.headers.set(name, value);
return this;
}
}
export class AbstractRequestOptionsWithMatchOptions extends AbstractRequestOptions {
ifMatch(...tags) {
this.addHeader('If-Match', tags.join(', '));
return this;
}
ifNoneMatch(...tags) {
this.addHeader('If-None-Match', tags.join(', '));
return this;
}
ifMatchAny() {
this.addHeader('If-Match', '*');
return this;
}
ifNoneMatchAny() {
this.addHeader('If-None-Match', '*');
return this;
}
}
export class DefaultMatchOptions extends AbstractRequestOptionsWithMatchOptions {
constructor() {
super();
}
/**
* Provides an instance of MatchOptions.
*
* @returns The MatchOptions
*/
static getInstance() {
return new DefaultMatchOptions();
}
}
export class MatchOptionsHelper {
static getWithMergeHeader(options) {
const optionsWithMergeHeader = options ? options : DefaultMatchOptions.getInstance();
return optionsWithMergeHeader.addHeader(Header.CONTENT_TYPE, ContentType.MERGE_PATCH_JSON);
}
}
export class DefaultFieldsOptions extends AbstractRequestOptionsWithMatchOptions {
constructor() {
super();
}
/**
* Provides an instance of FieldsOptions.
*
* @returns The FieldsOptions
*/
static getInstance() {
return new DefaultFieldsOptions();
}
withFields(...fields) {
super.addRequestParameter('fields', encodeURIComponent(fields.join()));
return this;
}
}
export class DefaultCountOptions extends AbstractRequestOptions {
constructor() {
super();
}
/**
* Provides an instance of DefaultCountOptions.
*
* @returns The DefaultCountOptions
*/
static getInstance() {
return new DefaultCountOptions();
}
withRawFilter(rawFilterString) {
this.addRequestParameter('filter', encodeURIComponent(rawFilterString));
return this;
}
withFilter(filter) {
this.addRequestParameter('filter', encodeURIComponent(filter.toString()));
return this;
}
withNamespaces(...namespaces) {
this.addRequestParameter('namespaces', encodeURIComponent(namespaces.join()));
return this;
}
}
export class DefaultSearchOptions extends AbstractRequestOptions {
constructor() {
super();
/**
* Map of name-value pairs to add to the request parameters as "option"
*/
this.optionParameters = new Map();
}
/**
* Provides an instance of DefaultSearchOptions.
*
* @returns The DefaultSearchOptions
*/
static getInstance() {
return new DefaultSearchOptions();
}
withNamespaces(...namespaces) {
this.addRequestParameter('namespaces', encodeURIComponent(namespaces.join()));
return this;
}
withFields(...fields) {
this.addRequestParameter('fields', encodeURIComponent(fields.join()));
return this;
}
withRawFilter(rawFilterString) {
this.addRequestParameter('filter', encodeURIComponent(rawFilterString));
return this;
}
withFilter(filter) {
this.addRequestParameter('filter', encodeURIComponent(filter.toString()));
return this;
}
withLimit(offset, count) {
if (this.optionParameters.has('cursor') || this.optionParameters.has('size')) {
throw new Error('Cursor/size and limit options cannot be set at the same time');
}
this.optionParameters.set('limit', `${offset},${count}`);
return this.setOption();
}
withSort(sortOperation) {
this.optionParameters.set(`sort`, sortOperation);
return this.setOption();
}
withCursor(cursor) {
if (this.optionParameters.has('limit')) {
throw new Error('Limit and cursor options cannot be set at the same time');
}
this.optionParameters.set(`cursor`, cursor);
return this.setOption();
}
withPageSize(pageSize) {
if (this.optionParameters.has('limit')) {
throw new Error('Limit and cursor options cannot be set at the same time');
}
this.optionParameters.set('size', pageSize.toFixed(0));
return this.setOption();
}
/**
* Constructs the 'option' option out of the values of limit and sort.
*
* @returns The instance of DefaultSearchOptions with the constructed option
*/
setOption() {
const parameter = Array.from(this.optionParameters.entries())
.map(([key, value]) => `${key}(${value})`)
.join(',');
this.addRequestParameter('option', encodeURIComponent(parameter));
return this;
}
}
export class DefaultGetThingsOptions extends AbstractRequestOptions {
constructor() {
super();
}
/**
* Provides an instance of GetThingsOptions.
*
* @returns The GetThingsOptions
*/
static getInstance() {
return new DefaultGetThingsOptions();
}
withFields(...fields) {
super.addRequestParameter('fields', encodeURIComponent(fields.join()));
return this;
}
setThingIds(ids) {
super.addRequestParameter('ids', encodeURIComponent(ids.join()));
return this;
}
}
export class DefaultMessagesOptions extends AbstractRequestOptions {
constructor() {
super();
}
/**
* Provides an instance of MessagesOptions.
*
* @returns The MessagesOptions
*/
static getInstance() {
return new DefaultMessagesOptions();
}
withTimeout(timeout) {
this.addRequestParameter('timeout', timeout.toString());
return this;
}
}
export class DefaultPostConnectionOptions extends AbstractRequestOptions {
constructor() {
super();
}
/**
* Provides an instance of DefaultPostConnectionOptions.
*
* @returns The DefaultPostConnectionOptions
*/
static getInstance() {
return new DefaultPostConnectionOptions();
}
/**
* Gets an instance of DefaultPostConnectionOptions with the dry-run parameter set to true to test Connections.
*
* @returns The instance of MessagesOptions
*/
static getDryRunInstance() {
return new DefaultPostConnectionOptions().asDryRun(true);
}
asDryRun(dryRun) {
this.addRequestParameter('dry-run', String(dryRun));
return this;
}
}
//# sourceMappingURL=request.options.js.map