Skip to content

Commit

Permalink
PutResponse: accept empty body
Browse files Browse the repository at this point in the history
Signed-off-by: Dirk Van Haerenborgh <dirk.vanhaerenborgh@aloxy.io>
  • Loading branch information
vhdirk committed Feb 4, 2021
1 parent 9fd7809 commit 7da862b
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 26 deletions.
11 changes: 5 additions & 6 deletions javascript/lib/api/src/client/handles/features.interfaces.ts
Expand Up @@ -68,7 +68,7 @@ export interface FeaturesHandle {
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Features if provided by the response
*/
putFeatures(features: Features, options?: MatchOptions): Promise<PutResponse<Features> | GenericResponse>;
putFeatures(features: Features, options?: MatchOptions): Promise<PutResponse<Features>>;

/**
* Adds or updates a Feature of this handle's Thing.
Expand All @@ -77,7 +77,7 @@ export interface FeaturesHandle {
* @param options - Options to use for the request.
* @returns A Promise for the response
*/
putFeature(feature: Feature, options?: MatchOptions): Promise<GenericResponse>;
putFeature(feature: Feature, options?: MatchOptions): Promise<PutResponse<Feature>>;

/**
* Adds or updates the definition of the specified Feature.
Expand All @@ -87,7 +87,7 @@ export interface FeaturesHandle {
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Definition if provided by the response
*/
putDefinition(featureId: string, definition: string[], options?: MatchOptions): Promise<PutResponse<string[]> | GenericResponse>;
putDefinition(featureId: string, definition: string[], options?: MatchOptions): Promise<PutResponse<string[]>>;

/**
* Adds or updates the properties of the specified Feature.
Expand All @@ -97,8 +97,7 @@ export interface FeaturesHandle {
* @param options - Options to use for the request.
* @returns A Promise for the response
*/
putProperties(featureId: string, properties: object, options?: MatchOptions): Promise<GenericResponse>;

putProperties(featureId: string, properties: object, options?: MatchOptions): Promise<PutResponse<Object>>;

/**
* Adds or updates a Property of the specified Feature.
Expand All @@ -109,7 +108,7 @@ export interface FeaturesHandle {
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Property if provided by the response
*/
putProperty(featureId: string, propertyPath: string, property: any, options?: MatchOptions): Promise<PutResponse<any> | GenericResponse>;
putProperty(featureId: string, propertyPath: string, property: any, options?: MatchOptions): Promise<PutResponse<any>>;

/**
* Deletes all Features of this handle's Thing.
Expand Down
10 changes: 5 additions & 5 deletions javascript/lib/api/src/client/handles/features.ts
Expand Up @@ -133,7 +133,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {
});
}

public putFeatures(features: Features, options?: MatchOptions): Promise<PutResponse<Features> | GenericResponse> {
public putFeatures(features: Features, options?: MatchOptions): Promise<PutResponse<Features>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: Features.fromObject,
Expand All @@ -144,7 +144,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {
});
}

public putFeature(feature: Feature, options?: MatchOptions): Promise<PutResponse<Feature> | GenericResponse> {
public putFeature(feature: Feature, options?: MatchOptions): Promise<PutResponse<Feature>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => Feature.fromObject(o, feature.id),
Expand All @@ -155,7 +155,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {
});
}

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

public putProperties(featureId: string, properties: object, options?: MatchOptions): Promise<PutResponse<Object> | GenericResponse> {
public putProperties(featureId: string, properties: object, options?: MatchOptions): Promise<PutResponse<Object>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => o,
Expand All @@ -178,7 +178,7 @@ export class DefaultFeaturesHandle implements FeaturesHandle {
}

public putProperty(featureId: string, propertyPath: string,
property: any, options?: MatchOptions): Promise<PutResponse<any>| GenericResponse> {
property: any, options?: MatchOptions): Promise<PutResponse<any>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => o,
Expand Down
14 changes: 7 additions & 7 deletions javascript/lib/api/src/client/handles/policies.ts
Expand Up @@ -355,7 +355,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Policy if provided by the response
*/
putPolicy(policy: Policy, options?: MatchOptions): Promise<PutResponse<Policy> | GenericResponse> {
putPolicy(policy: Policy, options?: MatchOptions): Promise<PutResponse<Policy>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => Policy.fromObject(o, policy.id),
Expand All @@ -373,7 +373,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
* @param options - Options to use for the request.
* @returns A Promise a response containing the new Entries if provided by the response
*/
putEntries(policyId: string, entries: Entries, options?: MatchOptions): Promise<GenericResponse> {
putEntries(policyId: string, entries: Entries, options?: MatchOptions): Promise<PutResponse<Entries>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: Entries.fromObject,
Expand All @@ -392,7 +392,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Entry if provided by the response
*/
putEntry(policyId: string, entry: Entry, options?: MatchOptions): Promise<PutResponse<Entry> | GenericResponse> {
putEntry(policyId: string, entry: Entry, options?: MatchOptions): Promise<PutResponse<Entry>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => Entry.fromObject(o, entry.id),
Expand All @@ -412,7 +412,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Subjects if provided by the response
*/
putSubjects(policyId: string, label: string, subjects: Subjects, options?: MatchOptions): Promise<GenericResponse> {
putSubjects(policyId: string, label: string, subjects: Subjects, options?: MatchOptions): Promise<PutResponse<Subjects>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: Subjects.fromObject,
Expand All @@ -432,7 +432,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Subject if provided by the response
*/
putSubject(policyId: string, label: string, subject: Subject, options?: MatchOptions): Promise<PutResponse<Subject> | GenericResponse> {
putSubject(policyId: string, label: string, subject: Subject, options?: MatchOptions): Promise<PutResponse<Subject>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => Subject.fromObject(o, subject.id),
Expand All @@ -452,7 +452,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
* @param options - Options to use for the request.
* @returns A Promise for a response containing the new Resources if provided by the response
*/
putResources(policyId: string, label: string, resources: Resources, options?: MatchOptions): Promise<GenericResponse> {
putResources(policyId: string, label: string, resources: Resources, options?: MatchOptions): Promise<PutResponse<Resources>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: Resources.fromObject,
Expand All @@ -475,7 +475,7 @@ export class DefaultPoliciesHandle implements PoliciesHandle {
putResource(policyId: string,
label: string,
resource: Resource,
options?: MatchOptions): Promise<PutResponse<Resource> | GenericResponse> {
options?: MatchOptions): Promise<PutResponse<Resource>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => Resource.fromObject(o, resource.id),
Expand Down
8 changes: 4 additions & 4 deletions javascript/lib/api/src/client/handles/things.ts
Expand Up @@ -162,7 +162,7 @@ export class DefaultThingsHandle implements WebSocketThingsHandle, HttpThingsHan
return this.changeThing('create', thing, options) as Promise<PutResponse<Thing>>;
}

public putAttributes(thingId: string, attributes: object, options?: MatchOptions): Promise<PutResponse<object> | GenericResponse> {
public putAttributes(thingId: string, attributes: object, options?: MatchOptions): Promise<PutResponse<object>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => o,
Expand All @@ -184,7 +184,7 @@ export class DefaultThingsHandle implements WebSocketThingsHandle, HttpThingsHan
});
}

public putPolicyId(thingId: string, policyId: string, options?: MatchOptions): Promise<PutResponse<string> | GenericResponse> {
public putPolicyId(thingId: string, policyId: string, options?: MatchOptions): Promise<PutResponse<string>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: String,
Expand All @@ -205,7 +205,7 @@ export class DefaultThingsHandle implements WebSocketThingsHandle, HttpThingsHan
});
}

public putAclEntry(thingId: string, aclEntry: AclEntry, options?: MatchOptions): Promise<PutResponse<AclEntry> | GenericResponse> {
public putAclEntry(thingId: string, aclEntry: AclEntry, options?: MatchOptions): Promise<PutResponse<AclEntry>> {
return this.requestFactory.fetchPutRequest({
verb: 'PUT',
parser: o => AclEntry.fromObject(o, aclEntry.id),
Expand All @@ -216,7 +216,7 @@ export class DefaultThingsHandle implements WebSocketThingsHandle, HttpThingsHan
});
}

private changeThing(verb: string, thing: Thing, options?: MatchOptions): Promise<PutResponse<Thing> | GenericResponse> {
private changeThing(verb: string, thing: Thing, options?: MatchOptions): Promise<PutResponse<Thing>> {
return this.requestFactory.fetchPutRequest({
verb,
parser: Thing.fromObject,
Expand Down
Expand Up @@ -38,14 +38,14 @@ export abstract class RequestSender {
* @param options - The options to use for the request.
* @returns A Promise for the specified object
*/
public fetchPutRequest<T>(options: ParseRequest<T>): Promise<PutResponse<T> | GenericResponse> {
public fetchPutRequest<T>(options: ParseRequest<T>): Promise<PutResponse<T>> {
return this.fetchRequest(options)
.then(response => {
if (response.status === 201) {
return new PutResponse(options.parser(response.body), response.status, response.headers);
}
if (response.status === 204) {
return response;
return new PutResponse<T>(null, response.status, response.headers);
}
return Promise.reject(`Received unknown status code: ${response.status}`);
});
Expand Down
4 changes: 2 additions & 2 deletions javascript/lib/api/src/model/response.ts
Expand Up @@ -27,7 +27,7 @@ export interface GenericResponse {
}

export class PutResponse<T> implements GenericResponse {
public constructor(private readonly _value: T,
public constructor(private readonly _value: T | null,
private readonly _status: number,
private readonly _headers: Map<string, string>) {
}
Expand All @@ -40,7 +40,7 @@ export class PutResponse<T> implements GenericResponse {
return !this.wasCreated();
}

get body(): T {
get body(): T | null {
return this._value;
}

Expand Down

0 comments on commit 7da862b

Please sign in to comment.