Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Migrate @feathersjs/feathers to TypeScript #1963

Merged
merged 1 commit into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ packages/authentication-oauth/lib
packages/configuration/lib
packages/commons/lib
packages/errors/lib
packages/feathers/lib
packages/tests/lib
packages/transport-commons/lib
6 changes: 2 additions & 4 deletions packages/commons/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@feathersjs/commons",
"version": "4.5.3",
"version": "4.5.4",
"description": "Shared Feathers utility functions",
"homepage": "https://feathersjs.com",
"keywords": [
Expand Down Expand Up @@ -40,9 +40,7 @@
"publishConfig": {
"access": "public"
},
"dependencies": {
"@feathersjs/hooks": "^0.4.0-alpha.0"
},
"dependencies": {},
"devDependencies": {
"@types/mocha": "^7.0.2",
"@types/node": "^14.0.1",
Expand Down
116 changes: 113 additions & 3 deletions packages/commons/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,114 @@
import * as hookUtils from './hooks';
// Removes all leading and trailing slashes from a path
export function stripSlashes (name: string) {
return name.replace(/^(\/+)|(\/+)$/g, '');
}

export * from './utils';
export const hooks = hookUtils;
export type KeyValueCallback<T> = (value: any, key: string) => T;

// A set of lodash-y utility functions that use ES6
export const _ = {
each (obj: any, callback: KeyValueCallback<void>) {
if (obj && typeof obj.forEach === 'function') {
obj.forEach(callback);
} else if (_.isObject(obj)) {
Object.keys(obj).forEach(key => callback(obj[key], key));
}
},

some (value: any, callback: KeyValueCallback<boolean>) {
return Object.keys(value)
.map(key => [ value[key], key ])
.some(([val, key]) => callback(val, key));
},

every (value: any, callback: KeyValueCallback<boolean>) {
return Object.keys(value)
.map(key => [ value[key], key ])
.every(([val, key]) => callback(val, key));
},

keys (obj: any) {
return Object.keys(obj);
},

values (obj: any) {
return _.keys(obj).map(key => obj[key]);
},

isMatch (obj: any, item: any) {
return _.keys(item).every(key => obj[key] === item[key]);
},

isEmpty (obj: any) {
return _.keys(obj).length === 0;
},

isObject (item: any) {
return (typeof item === 'object' && !Array.isArray(item) && item !== null);
},

isObjectOrArray (value: any) {
return typeof value === 'object' && value !== null;
},

extend (first: any, ...rest: any[]) {
return Object.assign(first, ...rest);
},

omit (obj: any, ...keys: string[]) {
const result = _.extend({}, obj);
keys.forEach(key => delete result[key]);
return result;
},

pick (source: any, ...keys: string[]) {
return keys.reduce((result: { [key: string]: any }, key) => {
if (source[key] !== undefined) {
result[key] = source[key];
}

return result;
}, {});
},

// Recursively merge the source object into the target object
merge (target: any, source: any) {
if (_.isObject(target) && _.isObject(source)) {
Object.keys(source).forEach(key => {
if (_.isObject(source[key])) {
if (!target[key]) {
Object.assign(target, { [key]: {} });
}

_.merge(target[key], source[key]);
} else {
Object.assign(target, { [key]: source[key] });
}
});
}
return target;
}
};

// Duck-checks if an object looks like a promise
export function isPromise (result: any) {
return _.isObject(result) &&
typeof result.then === 'function';
}

export function makeUrl (path: string, app: any = {}) {
const get = typeof app.get === 'function' ? app.get.bind(app) : () => {};
const env = get('env') || process.env.NODE_ENV;
const host = get('host') || process.env.HOST_NAME || 'localhost';
const protocol = (env === 'development' || env === 'test' || (env === undefined)) ? 'http' : 'https';
const PORT = get('port') || process.env.PORT || 3030;
const port = (env === 'development' || env === 'test' || (env === undefined)) ? `:${PORT}` : '';

path = path || '';

return `${protocol}://${host}${port}/${exports.stripSlashes(path)}`;
}

export function createSymbol (name: string) {
return typeof Symbol !== 'undefined' ? Symbol(name) : name;
}
114 changes: 0 additions & 114 deletions packages/commons/src/utils.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/commons/test/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ describe('module', () => {

assert.equal(typeof commons, 'object');
assert.equal(typeof commons.stripSlashes, 'function');
assert.equal(typeof commons.hooks, 'object');
assert.equal(typeof commons._, 'object');
});

Expand Down
3 changes: 2 additions & 1 deletion packages/feathers/.npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/
test/
tsconfig.json
Loading