
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
React中可复用的通用模块, 一些不常用的函数,或在全局函数里会引起奇异的函数没有引出到全局,可以使用$$.request | $$.store 等间接使用,或import { request } from 'cmn-utils',也可以直接使用 import request from 'cmn-utils/lib/request'
简单包装的Fetch
import $$ from 'cmn-utils';
// 发送请求
$$.send('/send')
$$.get('/get/1')
$$.post('/post')
$$.put('/put')
$$.del('/put/1')
{
method: 'POST', // default 'POST'
mode: 'cors',
cache: 'no-cache',
credentials: 'include',
headers: {
'content-type': 'application/json'
},
responseType: 'json', // text or blob or formData https://fetch.spec.whatwg.org/
prefix: '', // request prefix
beforeRequest: null, // before request check, return false or a rejected Promise will stop request
afterResponse: null, // after request hook
}
// 原始fetch 写法
fetch('http://httpbin.org/post', {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
name: 'weiq',
})
})
.then(function(response) {
if (response.status >= 200 && response.status < 300) {
return response.json()
}
throw new Error(response.statusText)
})
.then(function(json) {
// ...
})
// 等价于,直接使用send方法
$$.send('http://httpbin.org/post', {
method: 'POST',
data: {name: 'weiq'}
}).then(resp => {
console.log(resp.json) // {name: 'weiq'}
})
// 等价于,使用提供的post方法
$$.post('http://httpbin.org/post', {name: 'weiq'})
.then(resp => {
console.log(resp.json) // {name: 'weiq'}
})
// 提交form表单
$$.getform('/form', {name: 'weiq'}) // 将拼接到url后面
//.postform('/form', {name: 'weiq'}) // 将做为Form Data发送
.then(resp => {
console.log(resp.json) // {name: 'weiq'}
})
// 全局配置, 将会覆盖默认参数, 一般全局配置一次
$$.requestConfig('method', 'GET')
.requestConfig({
headers: {'content-type': 'application/json'},
prefix: '/api'
})
// 配置请求头
$$.requestHeaders('Accept', 'application/json') // key-value
.requestHeaders({ Accept: 'application/json' }) // json
// 用函数反回头
$$.requestHeaders(_ => ({
random: Math.random()
}))
$$.post('http://httpbin.org/post', {name: 'weiq'}, {
headers: {
'content-type': 'application/json'
},
responseType: 'json',
})
.then(resp => {
console.log(resp.json) // {name: 'weiq'}
})
简单包装的store2
$$.setStore("name", "abc")
.setStore("multi", {ip: '0.0.0.0'})
.setStore("age", 18)
.setStore({
a: 1,
b: 2,
c: true
})
$$.getStore("multi");
$$.removeStore('name')
$$.clearStore()
$$.store.getStoreAsync("name").then(value => console.log("async:", value));
$$.store.getStoreInfo();
$$.store
.getStoreInfoAsync(value => console.log(value))
.setStore('name', 'abc');
$$.store.session('name', 'abc'); // 存储到 sessionStorage中
$$.store.local('name', 'abc'); // 存储到 localStorage中
让组件可以发射、监听自定义事件, 有时我们可能需要让两个没有太大关系的组件间建立联系,比如增加商品到购物车这个场景,一般会在页面头部(Header组件)放置购物车的图标,商品列表(Goods组件)中有一个添加到购物车的图标,我们想点击这个图标时,让头部购物车图标显示+1的效果,有几种方案,其一是在头部定义一个changeCardNumber方法,然后把这个方法层层传递到商品列表(Goods)中,进行调用,这很容易出错,其二是点击商品列表中商品时,通过dispatch发出一个类型是changeCardNumber的action,这种方法我们不得不把Header包装一下才可以让它处理dispatch的能力, 最后如果我们用自定义事件可以更容易实现这个效果,即在Header中订阅一个changeCardNumber这个事件,在其它任何地方只有触发trigger这个事件就可以了,只需要注意不用的事件要及时移除。
$$.on('eventName', function(value) {
console.log(value)
});
$$.trigger('eventName');
$$.off('eventName');
$$.once('eventName', function(value) {
console.log(value)
});
FAQs
common func & req
The npm package trj-utils receives a total of 8 weekly downloads. As such, trj-utils popularity was classified as not popular.
We found that trj-utils 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.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.