refactor: Updating bundler function to follow the function name standard

This commit is contained in:
Justin 2019-05-25 17:19:03 -04:00
parent bbd0b09cd1
commit 3ad35f69fe
3 changed files with 17 additions and 27 deletions

View File

@ -1,11 +1,11 @@
import { bundler } from './infrastructure';
import * as APIServices from './services';
import { Bundler } from './infrastructure';
// All seperatly
// All separately
export * from './services';
// Groups
export const GroupsBundle = Bundler({
export const GroupsBundle = bundler({
Groups: APIServices.Groups,
GroupAccessRequests: APIServices.GroupAccessRequests,
GroupBadges: APIServices.GroupBadges,
@ -22,7 +22,7 @@ export const GroupsBundle = Bundler({
});
// Users
export const UsersBundle = Bundler({
export const UsersBundle = bundler({
Users: APIServices.Users,
UserCustomAttributes: APIServices.UserCustomAttributes,
UserEmails: APIServices.UserEmails,
@ -32,7 +32,7 @@ export const UsersBundle = Bundler({
});
// Projects
export const ProjectsBundle = Bundler({
export const ProjectsBundle = bundler({
Branches: APIServices.Branches,
Commits: APIServices.Commits,
CommitDiscussions: APIServices.CommitDiscussions,
@ -77,4 +77,4 @@ export const ProjectsBundle = Bundler({
});
// All initialized
export default Bundler(APIServices);
export const Gitlab = bundler(APIServices);

View File

@ -1,21 +0,0 @@
import { BaseService } from '.';
import { BaseModelContructorOptions } from './BaseService';
function Bundler<T extends { [K: string]: typeof BaseService }>(
services: T,
): new (baseOptions: BaseModelContructorOptions) => { [K in keyof T]: InstanceType<T[K]> } {
const combined = { ...services as object } as T;
interface BundleClass {
[K: string]: BaseService;
}
return class Bundle implements BundleClass {
[K: string]: any;
constructor(baseOptions: BaseModelContructorOptions) {
Object.keys(combined).forEach((serviceName) => {
this[serviceName] = new combined[serviceName](baseOptions);
});
}
} as temporaryAny;
}
export default Bundler;

View File

@ -0,0 +1,11 @@
export function bundler<T extends { [name: string]: Constructor }, P extends keyof T>(services: T) {
return (function Bundle(options?: any) {
Object.entries(services || {}).forEach(([name, ser]) => {
this[name] = new ser(options);
});
} as any) as Bundle<T, P>;
}
export function skipAllCaps(key, convert, options) {
return /^[A-Z0-9_]+$/.test(key) ? key : convert(key, options);
}