Skip to content

Commit

Permalink
fix(groups): create and update methods wrong return types
Browse files Browse the repository at this point in the history
  • Loading branch information
gdostie committed Oct 8, 2019
1 parent 2c8bc91 commit e3a7123
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 23 deletions.
26 changes: 5 additions & 21 deletions src/APICore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,36 @@ import {APIConfiguration} from './ConfigurationInterfaces';
import {ResponseHandler} from './handlers/ResponseHandlerInterfaces';
import handleResponse, {defaultResponseHandlers} from './handlers/ResponseHandlers';

function removeEmptyEntries(obj) {
return Object.keys(obj).reduce((memo, key) => {
const val = obj[key];
if (val && typeof val === 'object') {
memo[key] = removeEmptyEntries(obj);
} else if (val != null || val !== '') {
memo[key] = obj[key];
}
return memo;
}, {});
}

function convertToQueryString(parameters: any) {
return parameters ? `?${new URLSearchParams(Object.entries(removeEmptyEntries(parameters))).toString()}` : '';
}

export default class API {
static orgPlaceholder = '{organizationName}';

constructor(private config: APIConfiguration) {}

async get<T>(url: string, args: RequestInit = {method: 'get'}): Promise<T> {
async get<T = {}>(url: string, args: RequestInit = {method: 'get'}): Promise<T> {
return await this.request<T>(url, args);
}

async post<T>(
async post<T = {}>(
url: string,
body: any,
args: RequestInit = {method: 'post', body: JSON.stringify(body), headers: {'Content-Type': 'application/json'}}
): Promise<T> {
return await this.request<T>(url, args);
}

async postForm<T>(url: string, form: FormData, args: RequestInit = {method: 'post', body: form}): Promise<T> {
async postForm<T = {}>(url: string, form: FormData, args: RequestInit = {method: 'post', body: form}): Promise<T> {
return await this.request<T>(url, args);
}

async put<T>(
async put<T = {}>(
url: string,
body: any,
args: RequestInit = {method: 'put', body: JSON.stringify(body), headers: {'Content-Type': 'application/json'}}
): Promise<T> {
return await this.request<T>(url, args);
}

async delete<T = void>(url: string, args: RequestInit = {method: 'delete'}): Promise<T> {
async delete<T = {}>(url: string, args: RequestInit = {method: 'delete'}): Promise<T> {
return await this.request<T>(url, args);
}

Expand Down
4 changes: 2 additions & 2 deletions src/resources/Groups/Groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class Group extends Resource {
}

create(group: New<GroupModel, 'resourceId'>, options?: CreateGroupOptions) {
return this.api.post<GroupModel>(this.buildPath(Group.baseUrl, options), group);
return this.api.post<{id: string}>(this.buildPath(Group.baseUrl, options), group);
}

delete(groupId: string) {
Expand All @@ -23,6 +23,6 @@ export default class Group extends Resource {
}

update(group: GroupModel, options?: UpdateGroupOptions) {
return this.api.put<GroupModel>(this.buildPath(`${Group.baseUrl}/${group.id}`, options), group);
return this.api.put(this.buildPath(`${Group.baseUrl}/${group.id}`, options), group);
}
}

0 comments on commit e3a7123

Please sign in to comment.