Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Piti is a small cli framework developed with Typescript. You can develop reactive applications by Redux and RxJS in Piti.
Install
$ yarn add piti
or
$ npm i piti --save
hello.ts
import { Command } from 'piti';
@Command()
class HelloCommand {
name = 'hello';
description = 'The hello world command';
handle() {
console.log('Hello World!');
}
}
export default HelloCommand;
index.ts
import Piti from 'piti';
import './hello';
Piti.run({
scriptName: 'console-app',
});
Terminal
$ npx ts-node index.ts hello
Piti uses yargs for command arguments. Created command builder will be inject to command class constructor. So you can be detail your command arguments.
Example:
import { Command } from 'piti';
import { Argv, Arguments } from 'yargs';
@Command()
class LoginCommand {
name = 'login';
description = 'Loging to platformm';
before(builder: Argv) {
builder
.positional('username', {
type: 'string',
describe: 'The user name',
})
.positional('password', {
type: 'string',
describe: 'The user password',
});
}
handle(args: Arguments) {
console.log('username:', args.username, 'password:', args.password);
}
}
export default LoginCommand;
Terminal
$ npx ts-node index.ts login --username test@example.com --password 1234
For more advanced usage, please visit: http://yargs.js.org
With the @Command
decorator you can inject parameters into the command class constructor.
@Command({
inject: [auth, user],
})
class LoginCommand {
constructor(auth, user) {
// ...
}
}
You can manage state of objects using pure ReduxJS library. For this first of all, you should be configure the redux then pass the store to Piti.
Install Redux:
$ yarn add redux
Create store:
import Piti from 'piti';
import { createStore } from 'redux';
const store = createStore(reducers);
Piti.run({
scriptName: 'console-app',
store,
});
That's all.
Action creator
const fetchUser = (email: string) => async (dispatch) => {
try {
dispatch({ type: 'FETCH_USER_PENDING' });
const result = await fetch(/**api request**/);
dispatch({ type: 'FETCH_USER_FULFILLED', data: result });
} catch (e) {
dispatch({ type: 'FETCH_USER_REJECTED', error: e });
}
};
Reducer
const initialState = {
pending: false,
error: null,
user: null,
};
const userReducer = (state = initialState, action) => {
switch (action.type) {
case 'FETCH_USER_PENDING': {
return {
...state,
pending: true,
};
},
case 'FETCH_USER_FULFILLED': {
return {
...state,
pending: false,
user: action.data
}
},
case 'FETCH_USER_REJECTED': {
return {
error: action.error,
pending: false,
user: null,
}
}
default:
return state;
}
};
Command
import { Command, Subscribe, dispatch, getState } from 'piti';
@Command()
class CreateUserCommand {
name = 'create-user [email]';
description = 'Create a new user';
args = {};
@Subscribe('FETCH_USER_FULFILLED')
userAlreadyExists() {
console.log('The user already created!');
}
@Subscribe('FETCH_USER_REJECTED')
fetchUserRejected() {
const { user } = getState();
if (user.error.message === 'user not found') {
this.createNewUser();
}
}
@Subscribe('CREATE_USER_FULFILLED')
createUserFulfilled() {
console.log('User created!');
}
createNewUser() {
const { email } = this.args;
dispatch(createUser(email));
}
handle(args: Arguments) {
this.args = args;
dispatch(fetchUser(args.email));
}
}
Terminal
$ npx ts-node index.ts create-user test@example.com
import { Subject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';
@Command()
class CreateUserCommand {
name = 'fetch-users';
description = 'Fetch users and filter vip ones';
@Subscribe({
action: 'FETCH_USERS_FULFILLED',
observer(subject: Subject<any>): Observable<any> {
return subject.pipe(filter((user) => user.isVip));
},
})
fetchUsersFulfilled(result) {
console.log(result);
// [{name: 'Thor', isVip: true}]
}
handle() {
dispatch(fetchUsers());
}
}
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
FAQs
Piti is a small cli framework developed with typescript.
The npm package piti receives a total of 1 weekly downloads. As such, piti popularity was classified as not popular.
We found that piti demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.