Skip to content

Commit 77d5b73

Browse files
Merge pull request #26 from cabinetoffice/IDP-440-add-post-pr-call-on-node-sdk-api
Add post and get generic call on Node SDK Api
2 parents f62bcae + c5e318f commit 77d5b73

5 files changed

Lines changed: 71 additions & 19 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@co-digital/api-sdk",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "An API SDK for Node.JS applications in CO Digital.",
55
"homepage": "https://github.com/cabinetoffice/node-api-sdk#README.md",
66
"main": "./lib/index.js",

src/api-sdk/github/github.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ export class Github {
6666
return this.responseHandler<GitHubCollaboratorsPerRepo[]>(response, collaboratorsPerRepoMapping);
6767
}
6868

69+
// generic get and post functions
70+
71+
public async getData(url: string): Promise<ApiResponse<any> | ApiErrorResponse> {
72+
const response = await this.request.httpGet(url);
73+
return this.responseHandler<any>(response);
74+
}
75+
76+
public async postData(url: string, body: any): Promise<ApiResponse<any> | ApiErrorResponse> {
77+
const response = await this.request.httpPost(url, JSON.stringify(body));
78+
return this.responseHandler<any>(response);
79+
}
80+
6981
private responseHandler<T>(
7082
response: any,
7183
responseMap?: (body: any) => T

test/mock/data.mock.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const MOCK_HEADERS: ApiOptions = {
88

99
export const MOCK_REPOS = [
1010
{
11-
name: "repo1",
11+
name: 'repo1',
1212
archived: false,
1313
created_at: '2015-12-10T11:48:11Z',
1414
description: 'Best repo 1',
@@ -18,7 +18,7 @@ export const MOCK_REPOS = [
1818
visibility: 'public'
1919
},
2020
{
21-
name: "repo2",
21+
name: 'repo2',
2222
archived: false,
2323
created_at: '2018-12-10T11:48:11Z',
2424
description: 'Best repo 2',
@@ -31,7 +31,7 @@ export const MOCK_REPOS = [
3131

3232
export const MOCK_UNMAPPED_RESPONSE_REPO = [
3333
{
34-
name: "repo1",
34+
name: 'repo1',
3535
archived: false,
3636
created_at: '2015-12-10T11:48:11Z',
3737
description: 'Best repo 1',
@@ -42,7 +42,7 @@ export const MOCK_UNMAPPED_RESPONSE_REPO = [
4242
test: 'test'
4343
},
4444
{
45-
name: "repo2",
45+
name: 'repo2',
4646
archived: false,
4747
created_at: '2018-12-10T11:48:11Z',
4848
description: 'Best repo 2',
@@ -80,7 +80,7 @@ export const MOCK_FULL_RESPONSE_MEMBERS = [ ...MOCK_MEMBERS ];
8080

8181
export const MOCK_MEMBER_FETCH_RESPONSE = {
8282
httpStatusCode: 200,
83-
resource: MOCK_MEMBERS.map(({ repos_url, ...rest }) => rest)
83+
resource: MOCK_MEMBERS.map(({ repos_url: _repos_url, ...rest }) => rest)
8484
};
8585

8686
export const MOCK_TEAMS = [
@@ -104,7 +104,7 @@ export const MOCK_TEAMS = [
104104

105105
export const MOCK_TEAM_FETCH_RESPONSE = {
106106
httpStatusCode: 200,
107-
resource: MOCK_TEAMS.map(({ members_url, repositories_url, ...rest }) => rest)
107+
resource: MOCK_TEAMS.map(({ members_url: _members_url, repositories_url: _repositories_url, ...rest }) => rest)
108108
};
109109

110110
export const MOCK_MEMBERS_PER_TEAM = [
@@ -150,22 +150,22 @@ export const MOCK_COLLABORATORS_PER_REPO = [
150150

151151
export const MOCK_COLLABORATORS_PER_REPO_RESPONSE = {
152152
httpStatusCode: 200,
153-
resource: MOCK_COLLABORATORS_PER_REPO.map(({ other, ...rest }) => rest)
153+
resource: MOCK_COLLABORATORS_PER_REPO.map(({ other: _other, ...rest }) => rest)
154154
};
155155

156156
export const MOCK_ISSUE_BODY = {
157-
title: "Add member to the team",
158-
body: "Add member Bob to the team Alice.",
159-
assignees: ["IDC TEAM"],
160-
labels: ["GIT"]
157+
title: 'Add member to the team',
158+
body: 'Add member Bob to the team Alice.',
159+
assignees: ['IDC TEAM'],
160+
labels: ['GIT']
161161
};
162162

163163
export const MOCK_ISSUE_RESPONSE = {
164164
id: 1,
165165
number: 1347,
166-
state: "open",
167-
title: "Add member to the team",
168-
body: "Add member Bob to the team Alice."
166+
state: 'open',
167+
title: 'Add member to the team',
168+
body: 'Add member Bob to the team Alice.'
169169
};
170170

171171
export const MOCK_ERROR = {
@@ -177,9 +177,23 @@ export const MOCK_ERROR_RESPONSE = {
177177
errors: [MOCK_ERROR]
178178
};
179179

180-
export const MOCK_403_ERROR_MSG = { "message": "Must have admin rights to Repository." };
180+
export const MOCK_403_ERROR_MSG = { 'message': 'Must have admin rights to Repository.' };
181181

182182
export const MOCK_403_ERROR_RESPONSE = {
183183
httpStatusCode: 403,
184184
errors: [MOCK_403_ERROR_MSG]
185185
};
186+
187+
export const MOCK_GET = { data: 'test data' };
188+
189+
export const MOCK_GET_RESPONSE = {
190+
httpStatusCode: 200,
191+
resource: MOCK_GET
192+
};
193+
194+
export const MOCK_POST = { success: true };
195+
196+
export const MOCK_POST_RESPONSE = {
197+
httpStatusCode: 200,
198+
resource: MOCK_POST
199+
};

test/unit/api-sdk/github/github.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import {
2323
MOCK_FULL_RESPONSE_REPO,
2424
MOCK_FULL_RESPONSE_MEMBERS,
2525
MOCK_ISSUE_RESPONSE,
26-
MOCK_ISSUE_BODY
26+
MOCK_ISSUE_BODY,
27+
MOCK_GET,
28+
MOCK_GET_RESPONSE,
29+
MOCK_POST,
30+
MOCK_POST_RESPONSE
2731
} from '../../../mock/data.mock';
2832
import { HttpResponse } from '../../../../src/http-request/type';
2933

@@ -137,6 +141,28 @@ describe('Github sdk module test suites', () => {
137141
expect(result).toEqual(MOCK_COLLABORATORS_PER_REPO_RESPONSE);
138142
});
139143

144+
test('should return get data ', async () => {
145+
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_GET));
146+
147+
const url = 'https://api.github.com/test/get';
148+
const result = await github.getData(url);
149+
150+
expect(httpRequestMock.httpGet).toHaveBeenCalledWith(url);
151+
expect(result).toEqual(MOCK_GET_RESPONSE);
152+
});
153+
154+
test('should post data successfully', async () => {
155+
httpRequestMock.httpPost.mockResolvedValue(createMockHttpResponse(MOCK_POST));
156+
157+
const url = 'https://api.github.com/test/post';
158+
const body = { key: 'value' };
159+
160+
const result = await github.postData(url, body);
161+
162+
expect(httpRequestMock.httpPost).toHaveBeenCalledWith(url, JSON.stringify(body));
163+
expect(result).toEqual(MOCK_POST_RESPONSE);
164+
});
165+
140166
test('Should return an object with an error property', async () => {
141167
httpRequestMock.httpGet.mockResolvedValue(createMockHttpResponse(MOCK_TEAMS, 500, MOCK_ERROR));
142168

0 commit comments

Comments
 (0)