本文整理汇总了TypeScript中@rematch/core.createModel函数的典型用法代码示例。如果您正苦于以下问题:TypeScript createModel函数的具体用法?TypeScript createModel怎么用?TypeScript createModel使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了createModel函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createModel
const Gist = createModel({
state: {
gist: {},
comments: []
},
reducers: {
setGist: (state: IGistState, payload: any) => {
state.gist = payload
return state
},
setComments: (state: IGistState, payload: any[]) => {
return (state.comments = payload)
}
},
effects: {
async getGist(payload: string) {
try {
const { data } = await github.get(`/gists/${payload}`, {
headers: {
Accept: 'application/vnd.github.v3.html'
},
responseType: 'text'
})
for (const filename in data.files) {
if (data.files.hasOwnProperty(filename)) {
const file = data.files[filename]
const res = await github.post(
'/markdown',
{
text: '```' + file.language + '\n' + file.content + '\n```',
mode: 'markdown'
},
{ responseType: 'text' }
)
file.html = res.data
}
}
dispatch.gist.getComments(payload)
dispatch.gist.setGist(data)
} catch (err) {
console.error(err)
}
},
async getComments(payload: string) {
try {
const { data } = await github.get(`/gists/${payload}/comments`, {
headers: {
Accept: 'application/vnd.github.v3.html'
},
responseType: 'text'
})
dispatch.gist.setComments(data)
} catch (err) {
console.error(err)
}
}
}
})
示例2: createModel
const Todos = createModel({
state: {
meta: {
page: 1,
perPage: 10,
total: 0
},
todos: [],
labels: []
},
reducers: {
setTodos: (state: ITodoState, payload: ISetTodos) => {
state.todos = payload.todos
state.meta = payload.meta
return state
},
setLabel: (state: ITodoState, payload: any[]) => {
return (state.labels = payload)
}
},
effects: {
async getTodos(payload: IGetTodos) {
const { page, perPage } = payload.meta
try {
const res = await github.get(`/repos/${CONFIG.owner}/${CONFIG.todo_repo}/issues`, {
params: { creator: CONFIG.owner, page, per_page: perPage, state: 'all' }
})
const todos = res.data
const link = res.headers.link
let newMeta = payload.meta
if (link) {
const isLast = link.match(/<([^>]+)>(?=\;\s+rel="last")/)
const lastPage = isLast ? isLast[1].match(/page=(\d+)/)[1] : page
newMeta = {
...payload.meta,
...{ page, per_page: perPage, total: lastPage * perPage }
}
}
dispatch.todos.setTodos({ todos, meta: newMeta })
} catch (err) {
console.error(err)
}
},
async getLabels() {
const { data } = await github.get(`/repos/${CONFIG.owner}/${CONFIG.todo_repo}/labels`)
dispatch.todos.setLabel(data)
}
}
})
示例3: createModel
const Cases = createModel({
state: {
meta: {
page: 1,
perPage: 10,
total: 0
},
cases: []
},
reducers: {
setCases: (state: ICaseState, payload: ISetCase) => {
state.cases = payload.cases
state.meta = payload.meta
return state
}
},
effects: {
async getCases(payload: IGetCase) {
const { page, perPage } = payload.meta
try {
const { data, headers } = await github.get(`/repos/${CONFIG.owner}/showcase/issues`)
const link = headers.link
let newMeta = payload.meta
const cases: any[] = []
if (link) {
const isLast = link.match(/<([^>]+)>(?=\;\s+rel="last")/)
const lastPage = isLast ? isLast[1].match(/page=(\d+)/)[1] : page
newMeta = {
...payload.meta,
...{ page, per_page: perPage, total: lastPage * perPage }
}
}
data.forEach((c: any) => {
cases.push(dataParser(c))
})
dispatch.cases.setCases({ cases, meta: newMeta })
} catch (err) {
console.error(err)
}
}
}
})
示例4: createModel
const Repo = createModel({
state: {
repo: {},
readme: '',
events: []
},
reducers: {
setRepo: (state: IRepoState, payload: any) => {
return (state.repo = payload)
},
setReadme: (state: IRepoState, payload: string) => {
return (state.readme = payload)
},
setEvents: (state: IRepoState, payload: any[]) => {
return (state.events = payload)
}
},
effects: {
async getRepo(payload: string) {
try {
const { data = {} } = await github.get(`/repos/${CONFIG.owner}/${payload}`, {
headers: {
Accept: 'application/vnd.github.mercy-preview+json;charset=utf-8'
}
})
dispatch.repo.getReadme(payload)
dispatch.repo.getEvents(payload)
dispatch.repo.setRepo(data)
} catch (err) {
console.error(err)
}
},
async getReadme(payload: string) {
try {
const { data } = await github.get(`/repos/${CONFIG.owner}/${payload}/readme`, {
headers: {
Accept: 'application/vnd.github.v3.html'
},
responseType: 'text'
})
dispatch.repo.setReadme(data)
} catch (err) {
console.error(err)
}
},
async getEvents(payload: string) {
try {
const { data } = await github.get(`/repos/${CONFIG.owner}/${payload}/events`)
dispatch.repo.setEvents(data)
} catch (err) {
console.error(err)
}
}
}
})
示例5: createModel
const Posts = createModel({
state: {
meta: {
page: 1,
perPage: 10,
total: 0
},
posts: []
},
reducers: {
setPosts: (state: IPostState, payload: ISetPosts) => {
state.posts = payload.posts
state.meta = payload.meta
return state
}
},
effects: {
async getPosts(payload: IGetPosts) {
const { page, perPage } = payload.meta
try {
const res = await github.get(`/repos/${CONFIG.owner}/${CONFIG.repo}/issues`, {
params: { creator: CONFIG.owner, page, per_page: perPage, state: 'open' }
})
const posts = res.data
const link = res.headers.link
let newMeta = payload.meta
/**
* Pagination
* # see detail https://developer.github.com/guides/traversing-with-pagination/
*/
if (link) {
const isLast = link.match(/<([^>]+)>(?=\;\s+rel="last")/)
const lastPage = isLast ? isLast[1].match(/page=(\d+)/)[1] : page
newMeta = {
...payload.meta,
...{ page, per_page: perPage, total: lastPage * perPage }
}
}
posts.forEach((post: any) => {
// 获取第一张图片作为缩略图
const match = /!\[[^\]]+\]\(([^\)]+)\)/im.exec(post.body)
if (match && match[1]) {
post.thumbnails = match[1]
}
})
dispatch.posts.setPosts({ posts, meta: newMeta })
} catch (err) {
console.error(err)
}
}
}
})
示例6: createModel
todo: any
}
const Todos = createModel({
state: {
todo: {}
},
reducers: {
setTodo: (state: ITodoState, payload: any) => {
return (state.todo = payload)
}
},
effects: {
async getTodo(payload: string) {
try {
const { data } = await github.get(`/repos/${CONFIG.owner}/${CONFIG.todo_repo}/issues/${payload}`, {
headers: {
Accept: 'application/vnd.github.v3.html'
},
responseType: 'text'
})
dispatch.todo.setTodo(data)
} catch (err) {
console.error(err)
}
}
}
})
export default Todos
示例7: createModel
const Gists = createModel({
state: {
meta: {
page: 1,
perPage: 10,
total: 0
},
gists: []
},
reducers: {
setGists: (state: IGistState, payload: ISetGists) => {
state.gists = payload.gists
state.meta = payload.meta
return state
}
},
effects: {
async getGists(payload: IGetGists) {
const { page, perPage } = payload.meta
try {
const res = await github.get(`/users/${CONFIG.owner}/gists`, {
params: { page, per_page: perPage }
})
const gists = res.data
const link = res.headers.link
let newMeta = payload.meta
if (link) {
const isLast = link.match(/<([^>]+)>(?=\;\s+rel="last")/)
const lastPage = isLast ? isLast[1].match(/page=(\d+)/)[1] : page
newMeta = {
...payload.meta,
...{ page, per_page: perPage, total: lastPage * perPage }
}
}
dispatch.gists.setGists({ gists, meta: newMeta })
} catch (err) {
console.error(err)
}
}
}
})
示例8: createModel
}
const About = createModel({
state: {
about: ''
},
reducers: {
storeAboutMe: (state: IAboutState, payload: ISetAbout) => {
return (state.about = payload.about)
}
},
effects: {
async getAboutMe() {
let html = ''
try {
const response = await github.get(`/repos/${CONFIG.owner}/${CONFIG.repo}/contents/ABOUTME.md`, {
headers: {
Accept: 'application/vnd.github.v3.html'
},
responseType: 'text'
})
html = response.data
} catch (err) {
console.error(err)
}
dispatch.about.storeAboutMe(html)
}
}
})
export default About
示例9: createModel
const Repos = createModel({
state: {
meta: {
page: 1,
perPage: 10,
total: 0
},
repos: []
},
reducers: {
setRepos: (state: IRepoState, payload: ISetRepos) => {
state.repos = payload.repos
state.meta = payload.meta
return state
}
},
effects: {
async getRepos(payload: IGetRepos) {
const { page, perPage } = payload.meta
try {
const res = await github.get(`/users/${CONFIG.owner}/repos?sort=created`, {
params: { page, per_page: perPage },
headers: {
Accept: 'application/vnd.github.mercy-preview+json;charset=utf-8'
}
})
const repos = res.data
const link = res.headers.link
let newMeta = payload.meta
/**
* Pagination
* # see detail https://developer.github.com/guides/traversing-with-pagination/
*/
if (link) {
const isLast = link.match(/<([^>]+)>(?=\;\s+rel="last")/)
const lastPage = isLast ? isLast[1].match(/page=(\d+)/)[1] : page
newMeta = {
...payload.meta,
...{ page, per_page: perPage, total: lastPage * perPage }
}
}
dispatch.repos.setRepos({ repos, meta: newMeta })
} catch (err) {
console.error(err)
}
}
}
})
示例10: createModel
const Post = createModel({
state: {
post: {},
comments: []
},
reducers: {
setPost: (state: IPostState, payload: any) => {
return (state.post = payload)
},
setComments: (state: IPostState, payload: any[]) => {
return (state.comments = payload)
}
},
effects: {
async getPost(payload: number) {
let post: ISimpleObj = {}
try {
const { data } = await github.get(`/repos/${CONFIG.owner}/${CONFIG.repo}/issues/${payload}`, {
headers: {
Accept: 'application/vnd.github.v3.html'
},
responseType: 'text'
})
post = data
// post.filter_html = htmlFilter(data.body_html)
post.filter_html = data.body_html
} catch (err) {
console.error(err)
}
dispatch.post.setPost(post)
},
async getIssuesComments(payload: number) {
try {
const { data } = await github.get(`/repos/${CONFIG.owner}/${CONFIG.repo}/issues/${payload}/comments`, {
headers: {
Accept: 'application/vnd.github.v3.html'
},
responseType: 'text'
})
dispatch.post.setComments(data)
} catch (err) {
console.error(err)
}
}
}
})