Skip to content

Commit

Permalink
fix: Fixing resource variables API's #240
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple committed Nov 26, 2018
1 parent 29eff9f commit da162fd
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/services/GroupVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseServiceOptions } from '@typings';

class GroupVariables extends ResourceVariables {
constructor(options: BaseServiceOptions) {
super('groups', null, options);
super('groups', options);
}
}

Expand Down
87 changes: 81 additions & 6 deletions src/services/PipelineScheduleVariables.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,85 @@
import { ResourceVariables } from '../templates';
import { BaseServiceOptions } from '@typings';
import { BaseService, RequestHelper } from '../infrastructure';
import {
BaseRequestOptions,
PaginatedRequestOptions,
PipelineScheduleId,
ProjectId,
KeyId,
} from '@typings';

class PipelineScheduleVariables extends ResourceVariables {
constructor(options: BaseServiceOptions) {
super('projects', 'pipelines', options);
}
class PipelineScheduleVariables extends BaseService {
all(
projectId: ProjectId,
pipelineScheduleId: PipelineScheduleId,
options?: PaginatedRequestOptions,
) {
const [pId, psId] = [projectId, pipelineScheduleId].map(encodeURIComponent);

return RequestHelper.get(
this,
`projects/${pId}/pipeline_schedules/${psId}/variables`,
options,
);
}

create(
projectId: ProjectId,
pipelineScheduleId: PipelineScheduleId,
options?: BaseRequestOptions,
) {
const [pId, psId] = [projectId, pipelineScheduleId].map(encodeURIComponent);

return RequestHelper.post(
this,
`projects/${pId}/pipeline_schedules/${psId}/variables`,
options,
);
}

edit(
projectId: ProjectId,
pipelineScheduleId: PipelineScheduleId,
keyId: KeyId,
options?: BaseRequestOptions,
) {
const [pId, psId, kId] = [projectId, pipelineScheduleId, keyId].map(encodeURIComponent);

return RequestHelper.put(
this,
`projects/${pId}/pipeline_schedules/${psId}/variables/${kId}`,
options,
);
}

show(
projectId: ProjectId,
pipelineScheduleId: PipelineScheduleId,
keyId: KeyId,
options?: BaseRequestOptions,
) {
const [pId, psId, kId] = [projectId, pipelineScheduleId, keyId].map(encodeURIComponent);

return RequestHelper.get(
this,
`projects/${pId}/pipeline_schedules/${psId}/variables/${kId}`,
options,
);
}

remove(
projectId: ProjectId,
pipelineScheduleId: PipelineScheduleId,
keyId: KeyId,
options?: BaseRequestOptions,
) {
const [pId, psId, kId] = [projectId, pipelineScheduleId, keyId].map(encodeURIComponent);

return RequestHelper.delete(
this,
`projects/${pId}/pipeline_schedules/${psId}/variables/${kId}`,
options,
);
}
}

export default PipelineScheduleVariables;
2 changes: 1 addition & 1 deletion src/services/ProjectVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseServiceOptions } from '@typings';

class ProjectVariables extends ResourceVariables {
constructor(options: BaseServiceOptions) {
super('projects', null, options);
super('projects', options);
}
}

Expand Down
78 changes: 18 additions & 60 deletions src/templates/ResourceVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,39 @@ import {
KeyId,
} from '@typings';

const url = (resourceId, resource2Type, resource2Id) => {
const [rId, r2Id] = [resourceId, resource2Id].map(encodeURIComponent);
const output = [rId];

if (resource2Id) output.push(resource2Type, r2Id);

output.push('variables');

return output.join('/');
};

class ResourceVariables extends BaseService {
protected resource2Type: string | null;

constructor(
resourceType: ResourceType,
resource2Type: ResourceType | null,
options: BaseServiceOptions,
) {
constructor(resourceType: ResourceType, options: BaseServiceOptions) {
super({ url: resourceType, ...options });

this.resource2Type = resource2Type;
}

all(resourceId: ResourceType, resource2Id: ResourceId, options?: PaginatedRequestOptions) {
return RequestHelper.get(this, url(resourceId, this.resource2Type, resource2Id), options);
all(resourceId: ResourceId, options?: PaginatedRequestOptions) {
const rId = encodeURIComponent(resourceId);

return RequestHelper.get(this, `${rId}/variables`, options);
}

create(resourceId: ResourceType, resource2Id: ResourceId, options?: BaseRequestOptions) {
return RequestHelper.post(this, url(resourceId, this.resource2Type, resource2Id), options);
create(resourceId: ResourceId, options?: BaseRequestOptions) {
const rId = encodeURIComponent(resourceId);

return RequestHelper.post(this, `${rId}/variables`, options);
}

edit(
resourceId: ResourceType,
resource2Id: ResourceId,
keyId: KeyId,
options?: BaseRequestOptions,
) {
const kId = encodeURIComponent(keyId);
edit(resourceId: ResourceId, keyId: KeyId, options?: BaseRequestOptions) {
const [kId, rId] = [resourceId, keyId].map(encodeURIComponent);

return RequestHelper.put(
this,
`${url(resourceId, this.resource2Type, resource2Id)}/${kId}`,
options,
);
return RequestHelper.put(this, `${rId}/variables/${kId}`, options);
}

show(
resourceId: ResourceType,
resource2Id: ResourceId,
keyId: KeyId,
options?: BaseRequestOptions,
) {
const kId = encodeURIComponent(keyId);
show(resourceId: ResourceId, keyId: KeyId, options?: PaginatedRequestOptions) {
const [kId, rId] = [resourceId, keyId].map(encodeURIComponent);

return RequestHelper.get(
this,
`${url(resourceId, this.resource2Type, resource2Id)}/${kId}`,
options,
);
return RequestHelper.get(this, `${rId}/variables/${kId}`, options);
}

remove(
resourceId: ResourceType,
resource2Id: ResourceId,
keyId: KeyId,
options?: BaseRequestOptions,
) {
const kId = encodeURIComponent(keyId);
remove(resourceId: ResourceId, keyId: KeyId, options?: PaginatedRequestOptions) {
const [kId, rId] = [resourceId, keyId].map(encodeURIComponent);

return RequestHelper.delete(
this,
`${url(resourceId, this.resource2Type, resource2Id)}/${kId}`,
options,
);
return RequestHelper.delete(this, `${rId}/variables/${kId}`, options);
}
}

Expand Down

0 comments on commit da162fd

Please sign in to comment.