Skip to content

Commit 97dd060

Browse files
committed
refactor: Similar to the RepositoryFiles API changes
Removed the dependency on FS for better browser support. BREAKING CHANGE: Removed dependency on FS. Now the Projects API takes in two arguments `projectId` and `content` as well as an option fileName argument
1 parent 6ea90d3 commit 97dd060

File tree

1 file changed

+50
-61
lines changed

1 file changed

+50
-61
lines changed

src/services/Projects.ts

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,152 +1,141 @@
1-
import Fs from 'fs';
2-
import Path from 'path';
1+
import FormData from 'form-data';
2+
import randomstring from 'randomstring';
33
import { BaseService, RequestHelper } from '../infrastructure';
4-
import { assertEventOptions } from './Events';
5-
import { RequestOptions } from '../infrastructure/RequestHelper';
6-
7-
/** TODO annotate options */
8-
type ProjectOptions = temporaryAny;
94

105
class Projects extends BaseService {
11-
all(options?: RequestOptions) {
6+
all(options?: PaginatedRequestOptions) {
127
return RequestHelper.get(this, 'projects', options);
138
}
149

15-
archive(projectId: ProjectId) {
10+
archive(projectId: ProjectId, options?: Sudo) {
1611
const pId = encodeURIComponent(projectId);
1712

18-
return RequestHelper.post(this, `projects/${pId}/archive`);
13+
return RequestHelper.post(this, `projects/${pId}/archive`, options);
1914
}
20-
/**
21-
* @see https://docs.gitlab.com/ee/api/projects.html#create-project-for-user
22-
*/
23-
create(options: temporaryAny) {
24-
const url = options.userId ? `projects/user/${encodeURIComponent(options.userId)}` : 'projects';
15+
16+
create({ userId, ...options }: { userId?: UserId } & BaseRequestOptions) {
17+
const url = userId ? `projects/user/${encodeURIComponent(userId)}` : 'projects';
2518

2619
return RequestHelper.post(this, url, options);
2720
}
2821

29-
edit(projectId: ProjectId, options: temporaryAny) {
22+
edit(projectId: ProjectId, options?: BaseRequestOptions) {
3023
const pId = encodeURIComponent(projectId);
3124

3225
return RequestHelper.put(this, `projects/${pId}`, options);
3326
}
3427

35-
events(projectId: ProjectId, options: ProjectOptions) {
36-
assertEventOptions(options.action, options.targetType);
37-
28+
events(projectId: ProjectId, options?: BaseRequestOptions & EventOptions) {
3829
const pId = encodeURIComponent(projectId);
3930

4031
return RequestHelper.get(this, `projects/${pId}/events`, options);
4132
}
4233

43-
fork(projectId: ProjectId, options: ProjectOptions) {
34+
fork(projectId: ProjectId, options?: BaseRequestOptions) {
4435
const pId = encodeURIComponent(projectId);
4536

4637
return RequestHelper.post(this, `projects/${pId}/fork`, options);
4738
}
4839

49-
forks(projectId: ProjectId, options: ProjectOptions) {
40+
forks(projectId: ProjectId, options?: BaseRequestOptions) {
5041
const pId = encodeURIComponent(projectId);
5142

5243
return RequestHelper.get(this, `projects/${pId}/forks`, options);
5344
}
5445

55-
languages(projectId: ProjectId) {
46+
languages(projectId: ProjectId, options?: Sudo) {
47+
const pId = encodeURIComponent(projectId);
48+
49+
return RequestHelper.get(this, `projects/${pId}/languages`, options);
50+
}
51+
52+
mirrorPull(projectId: ProjectId, options?: Sudo) {
5653
const pId = encodeURIComponent(projectId);
5754

58-
return RequestHelper.get(this, `projects/${pId}/languages`);
55+
return RequestHelper.post(this, `projects/${pId}/mirror/pull`, options);
5956
}
6057

61-
mirrorPull(projectId: ProjectId) {
58+
remove(projectId: ProjectId, options?: Sudo) {
6259
const pId = encodeURIComponent(projectId);
6360

64-
return RequestHelper.post(this, `projects/${pId}/mirror/pull`);
61+
return RequestHelper.del(this, `projects/${pId}`, options);
6562
}
6663

67-
remove(projectId: ProjectId) {
64+
removeFork(projectId: ProjectId, options?: Sudo) {
6865
const pId = encodeURIComponent(projectId);
6966

70-
return RequestHelper.delete(this, `projects/${pId}`);
67+
return RequestHelper.del(this, `projects/${pId}/fork`, options);
7168
}
7269

7370
search(projectName: string) {
7471
return RequestHelper.get(this, 'projects', { search: projectName });
7572
}
7673

77-
share(projectId: ProjectId, groupId: GroupId, groupAccess: GroupAccess, options: ProjectOptions) {
74+
share(projectId: ProjectId, groupId: GroupId, groupAccess: number, options?: BaseRequestOptions) {
7875
const pId = encodeURIComponent(projectId);
7976

80-
if (!groupId || !groupAccess) throw new Error('Missing required arguments');
81-
8277
return RequestHelper.post(this, `projects/${pId}/share`, { groupId, groupAccess, ...options });
8378
}
8479

85-
show(projectId: ProjectId, options: ProjectOptions) {
80+
show(projectId: ProjectId, options?: BaseRequestOptions) {
8681
const pId = encodeURIComponent(projectId);
8782

8883
return RequestHelper.get(this, `projects/${pId}`, options);
8984
}
9085

91-
star(projectId: ProjectId) {
86+
star(projectId: ProjectId, options?: Sudo) {
9287
const pId = encodeURIComponent(projectId);
9388

94-
return RequestHelper.post(this, `projects/${pId}/star`);
89+
return RequestHelper.post(this, `projects/${pId}/star`, options);
9590
}
9691

97-
statuses(projectId: ProjectId, sha: string, state: string, options: ProjectOptions) {
92+
statuses(projectId: ProjectId, sha: string, state: string, options?: BaseRequestOptions) {
9893
const pId = encodeURIComponent(projectId);
9994

10095
return RequestHelper.post(this, `projects/${pId}/statuses/${sha}`, { state, ...options });
10196
}
10297

103-
transfer(projectId: ProjectId, namespace: string) {
98+
transfer(projectId: ProjectId, namespaceId: NamespaceId) {
10499
const pId = encodeURIComponent(projectId);
105-
return RequestHelper.put(this, `projects/${pId}/transfer`, { namespace });
100+
return RequestHelper.put(this, `projects/${pId}/transfer`, { namespace: namespaceId });
106101
}
107102

108-
unarchive(projectId: ProjectId) {
103+
unarchive(projectId: ProjectId, options?: Sudo) {
109104
const pId = encodeURIComponent(projectId);
110105

111-
return RequestHelper.post(this, `projects/${pId}/unarchive`);
106+
return RequestHelper.post(this, `projects/${pId}/unarchive`, options);
112107
}
113108

114-
unshare(projectId: ProjectId, groupId: GroupId) {
109+
unshare(projectId: ProjectId, groupId: GroupId, options?: Sudo) {
115110
const [pId, gId] = [projectId, groupId].map(encodeURIComponent);
116111

117-
return RequestHelper.delete(this, `projects/${pId}/share${gId}`);
112+
return RequestHelper.del(this, `projects/${pId}/share/${gId}`, options);
118113
}
119114

120-
unstar(projectId: ProjectId) {
115+
unstar(projectId: ProjectId, options?: Sudo) {
121116
const pId = encodeURIComponent(projectId);
122117

123-
return RequestHelper.post(this, `projects/${pId}/unstar`);
118+
return RequestHelper.post(this, `projects/${pId}/unstar`, options);
124119
}
125120

126-
updatePushRule(projectId: ProjectId, options: ProjectOptions) {
121+
updatePushRule(projectId: ProjectId, options?: BaseRequestOptions) {
127122
const pId = encodeURIComponent(projectId);
128123

129124
return RequestHelper.put(this, `projects/${pId}/push_rule`, options);
130125
}
131126

132-
upload(projectId: ProjectId, filePath: string, { fileName = Path.basename(filePath) } = {}) {
133-
const pId = encodeURIComponent(projectId);
134-
const file = Fs.readFileSync(filePath);
135-
136-
return RequestHelper.post(
137-
this,
138-
`projects/${pId}/uploads`,
139-
{
140-
file: {
141-
value: file,
142-
options: {
143-
filename: fileName,
144-
contentType: 'application/octet-stream',
145-
},
146-
},
147-
},
148-
true,
149-
);
127+
upload(projectId, content, metadata: ProjectUploadMetadata = {}, options?: Sudo) {
128+
const pId = encodeURIComponent(projectId);
129+
const form = new FormData();
130+
131+
const defaultMetadata: ProjectUploadMetadata = {
132+
filename: randomstring.generate(8),
133+
contentType: 'application/octet-stream',
134+
};
135+
136+
form.append('file', content, Object.assign(defaultMetadata, metadata));
137+
138+
return RequestHelper.post(this, `projects/${pId}/uploads`, { ...options, form });
150139
}
151140
}
152141

0 commit comments

Comments
 (0)