Skip to content

Commit

Permalink
added merge property to javascript client sdk
Browse files Browse the repository at this point in the history
Signed-off-by: Pieter-Jan Lanneer <pj.lanneer@gmail.com>
  • Loading branch information
PJGitLan committed Feb 28, 2023
1 parent a9b7abe commit 2249354
Show file tree
Hide file tree
Showing 29 changed files with 808 additions and 185 deletions.
18 changes: 18 additions & 0 deletions javascript/lib/api/src/client/constants/content-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*!
* Copyright (c) 2023 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
*/

export enum ContentType {
MERGE_PATCH_JSON = 'application/merge-patch+json',
JSON = 'application/json',
TEXT = 'text/plain'
}
20 changes: 20 additions & 0 deletions javascript/lib/api/src/client/constants/ditto-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*!
* Copyright (c) 2023 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
*/

export enum DittoAction {
CREATE = 'create',
RETRIEVE = 'retrieve',
MODIFY = 'modify',
MERGE = 'merge',
DELETE = 'delete'
}
16 changes: 16 additions & 0 deletions javascript/lib/api/src/client/constants/header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*!
* Copyright (c) 2023 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
*/

export enum Header {
CONTENT_TYPE = 'Content-Type'
}
20 changes: 20 additions & 0 deletions javascript/lib/api/src/client/constants/http-verb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*!
* Copyright (c) 2023 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
*/

export enum HttpVerb {
GET = 'GET',
POST = 'POST',
PUT = 'PUT',
PATCH = 'PATCH',
DELETE = 'DELETE'
}
49 changes: 49 additions & 0 deletions javascript/lib/api/src/client/handles/features.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ export interface FeaturesHandle {
*/
putFeatures(features: Features, options?: MatchOptions): Promise<PutResponse<Features>>;

/**
* Merges a Feature of this handle's Thing.
*
* @param feature - The patched Feature.
* @param options - Options to use for the request.
* @returns A Promise for the response
*/
patchFeature(feature: Feature, options?: MatchOptions): Promise<PutResponse<Feature>>;

/**
* Merges all Features of this handle's Thing.
*
* @param features - The patched Features.
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Features if provided by the response
*/
patchFeatures(features: Features, options?: MatchOptions): Promise<PutResponse<Features>>;

/**
* Adds or updates a Feature of this handle's Thing.
*
Expand All @@ -89,6 +107,16 @@ export interface FeaturesHandle {
*/
putDefinition(featureId: string, definition: string[], options?: MatchOptions): Promise<PutResponse<string[]>>;

/**
* Merges the definition of the specified Feature.
*
* @param featureId - The ID of the Feature.
* @param definition - The patched definition.
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Definition if provided by the response
*/
patchDefinition(featureId: string, definition: string[], options?: MatchOptions): Promise<PutResponse<string[]>>;

/**
* Adds or updates the properties of the specified Feature.
*
Expand All @@ -110,6 +138,27 @@ export interface FeaturesHandle {
*/
putProperty(featureId: string, propertyPath: string, property: any, options?: MatchOptions): Promise<PutResponse<any>>;

/**
* Merges the properties of the specified Feature.
*
* @param featureId - The ID of the Feature.
* @param properties - The patched properties.
* @param options - Options to use for the request.
* @returns A Promise for the response
*/
patchProperties(featureId: string, properties: object, options?: MatchOptions): Promise<PutResponse<Object>>;

/**
* Merge a Property of the specified Feature.
*
* @param featureId - The ID of the Feature.
* @param propertyPath - The path to the Property to patch.
* @param property - The patched Property.
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Property if provided by the response
*/
patchProperty(featureId: string, propertyPath: string, property: any, options?: MatchOptions): Promise<PutResponse<any>>;

/**
* Deletes all Features of this handle's Thing.
*
Expand Down
88 changes: 72 additions & 16 deletions javascript/lib/api/src/client/handles/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
/* tslint:disable:no-nested-template-literals */
import { GenericResponse, PutResponse } from '../../model/response';
import { Feature, Features } from '../../model/things.model';
import { FieldsOptions, MatchOptions } from '../../options/request.options';
import { FieldsOptions, MatchOptions, MatchOptionsHelper } from '../../options/request.options';
import { HttpVerb } from '../constants/http-verb';
import { RequestSender, RequestSenderFactory } from '../request-factory/request-sender';
import { FeaturesHandle } from './features.interfaces';

Expand All @@ -40,7 +41,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public getFeatures(options?: FieldsOptions): Promise<Features> {
return this.requestFactory.fetchJsonRequest({
verb: 'GET',
verb: HttpVerb.GET,
parser: Features.fromObject,
id: this.thingId,
path: 'features',
Expand All @@ -50,7 +51,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public getFeature(featureId: string, options?: FieldsOptions): Promise<Feature> {
return this.requestFactory.fetchJsonRequest({
verb: 'GET',
verb: HttpVerb.GET,
parser: o => Feature.fromObject(o, featureId),
id: this.thingId,
path: `features/${featureId}`,
Expand All @@ -60,7 +61,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public getDefinition(featureId: string, options?: MatchOptions): Promise<string[]> {
return this.requestFactory.fetchJsonRequest({
verb: 'GET',
verb: HttpVerb.GET,
parser: o => Object(o).map((obj: any) => String(obj)),
id: this.thingId,
path: `features/${featureId}/definition`,
Expand All @@ -70,7 +71,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public getProperties(featureId: string, options?: FieldsOptions): Promise<Object> {
return this.requestFactory.fetchJsonRequest({
verb: 'GET',
verb: HttpVerb.GET,
parser: o => o,
id: this.thingId,
path: `features/${featureId}/properties`,
Expand All @@ -80,7 +81,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public getProperty(featureId: string, propertyPath: string, options?: MatchOptions): Promise<any> {
return this.requestFactory.fetchJsonRequest({
verb: 'GET',
verb: HttpVerb.GET,
parser: o => o,
id: this.thingId,
path: `features/${featureId}/properties/${propertyPath}`,
Expand All @@ -90,7 +91,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public deleteFeatures(options?: MatchOptions): Promise<GenericResponse> {
return this.requestFactory.fetchRequest({
verb: 'DELETE',
verb: HttpVerb.DELETE,
id: this.thingId,
path: 'features',
requestOptions: options
Expand All @@ -99,7 +100,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public deleteFeature(featureId: string, options?: MatchOptions): Promise<GenericResponse> {
return this.requestFactory.fetchRequest({
verb: 'DELETE',
verb: HttpVerb.DELETE,
id: this.thingId,
path: `features/${featureId}`,
requestOptions: options
Expand All @@ -108,7 +109,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public deleteDefinition(featureId: string, options?: MatchOptions): Promise<GenericResponse> {
return this.requestFactory.fetchRequest({
verb: 'DELETE',
verb: HttpVerb.DELETE,
id: this.thingId,
path: `features/${featureId}/definition`,
requestOptions: options
Expand All @@ -117,7 +118,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public deleteProperties(featureId: string, options?: MatchOptions): Promise<GenericResponse> {
return this.requestFactory.fetchRequest({
verb: 'DELETE',
verb: HttpVerb.DELETE,
id: this.thingId,
path: `features/${featureId}/properties`,
requestOptions: options
Expand All @@ -126,7 +127,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public deleteProperty(featureId: string, propertyPath: string, options?: MatchOptions): Promise<GenericResponse> {
return this.requestFactory.fetchRequest({
verb: 'DELETE',
verb: HttpVerb.DELETE,
id: this.thingId,
path: `features/${featureId}/properties/${propertyPath}`,
requestOptions: options
Expand All @@ -135,7 +136,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public putFeatures(features: Features, options?: MatchOptions): Promise<PutResponse<Features>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
verb: HttpVerb.PUT,
parser: Features.fromObject,
id: this.thingId,
path: 'features',
Expand All @@ -146,7 +147,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {

public putFeature(feature: Feature, options?: MatchOptions): Promise<PutResponse<Feature>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
verb: HttpVerb.PUT,
parser: o => Feature.fromObject(o, feature.id),
id: this.thingId,
path: `features/${feature.id}`,
Expand All @@ -155,9 +156,31 @@ export class DefaultFeaturesHandle implements FeaturesHandle {
});
}

public patchFeatures(features: Features, options?: MatchOptions): Promise<PutResponse<Features>> {
return this.requestFactory.fetchPutRequest({
verb: HttpVerb.PATCH,
parser: Features.fromObject,
id: this.thingId,
path: 'features',
requestOptions: MatchOptionsHelper.getWithMergeHeader(options),
payload: Features.toObject(features)
});
}

public patchFeature(feature: Feature, options?: MatchOptions): Promise<PutResponse<Feature>> {
return this.requestFactory.fetchPutRequest({
verb: HttpVerb.PATCH,
parser: o => Feature.fromObject(o, feature.id),
id: this.thingId,
path: `features/${feature.id}`,
requestOptions: MatchOptionsHelper.getWithMergeHeader(options),
payload: feature.toObject()
});
}

public putDefinition(featureId: string, definition: string[], options?: MatchOptions): Promise<PutResponse<string[]>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
verb: HttpVerb.PUT,
parser: o => o !== undefined ? Object.values(o).map((obj: any) => String(obj)) : [],
id: this.thingId,
path: `features/${featureId}/definition`,
Expand All @@ -166,9 +189,20 @@ export class DefaultFeaturesHandle implements FeaturesHandle {
}); // `[${String(definition.map(s => `"${s}"`))}]`
}

public patchDefinition(featureId: string, definition: string[], options?: MatchOptions): Promise<PutResponse<string[]>> {
return this.requestFactory.fetchPutRequest({
verb: HttpVerb.PATCH,
parser: o => o !== undefined ? Object.values(o).map((obj: any) => String(obj)) : [],
id: this.thingId,
path: `features/${featureId}/definition`,
requestOptions: MatchOptionsHelper.getWithMergeHeader(options),
payload: definition
});
}

public putProperties(featureId: string, properties: object, options?: MatchOptions): Promise<PutResponse<Object>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
verb: HttpVerb.PUT,
parser: o => o,
id: this.thingId,
path: `features/${featureId}/properties`,
Expand All @@ -180,13 +214,35 @@ export class DefaultFeaturesHandle implements FeaturesHandle {
public putProperty(featureId: string, propertyPath: string,
property: any, options?: MatchOptions): Promise<PutResponse<any>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
verb: HttpVerb.PUT,
parser: o => o,
id: this.thingId,
path: `features/${featureId}/properties/${propertyPath}`,
requestOptions: options,
payload: property
});
}

public patchProperties(featureId: string, properties: object, options?: MatchOptions): Promise<PutResponse<Object>> {
return this.requestFactory.fetchPutRequest({
verb: HttpVerb.PATCH,
parser: o => o,
id: this.thingId,
path: `features/${featureId}/properties`,
requestOptions: MatchOptionsHelper.getWithMergeHeader(options),
payload: properties
});
}

public patchProperty(featureId: string, propertyPath: string, property: any, options?: MatchOptions): Promise<PutResponse<any>> {
return this.requestFactory.fetchPutRequest({
verb: HttpVerb.PATCH,
parser: o => o,
id: this.thingId,
path: `features/${featureId}/properties/${propertyPath}`,
requestOptions: MatchOptionsHelper.getWithMergeHeader(options),
payload: property
});
}
}

Loading

0 comments on commit 2249354

Please sign in to comment.