restful

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PageLinkHeaderKey   = "Link"
	PageInfoHeaderKey   = "X-Pagination-Info"
	TotalCountHeaderKey = "X-PaginateTotal-Count"
	HasMoreHeaderKey    = "X-More-Resource"
	ErrorCodeHeaderKey  = "X-Error-Code"
	ErrorDataHeaderKey  = "X-Error-Data"
	LangHeaderKey       = "X-Language"
)

Variables

This section is empty.

Functions

func WithResponseDefaultLanguage

func WithResponseDefaultLanguage(lang language.Tag) func(*response)

WithResponseDefaultLanguage 设置默认语言

func WithResponseErrorMsgHandler

func WithResponseErrorMsgHandler(supported []language.Tag, handle func(code int, lang language.Tag) string) func(*response)

WithResponseErrorMsgHandler 指定错误消息处理器。 主要应用于多国语言展示错误信息。 其中,`code` 为错误码;`language` 为标准的 i18n 标识

func WithResponseLanguageHeaderKey

func WithResponseLanguageHeaderKey(languageHeaderKey string) func(*response)

WithResponseLanguageHeaderKey 设置多国语言 header 参数 key, 默认为 `X-Language`。 主要应用于多国语言展示错误信息。

func WithResponseLogger

func WithResponseLogger(logger xlog.XLog) func(*response)

WithResponseLogger 指定 Logger

func WithResponseShowXCode

func WithResponseShowXCode(xcodes ...xcode.XCode) func(*response)

WithResponseShowXCode 设置需要对客户端展示的错误码。 默认情况下,该值为空,则表示所有错误码均向用户展示(设置为空,亦如此);

Types

type IResponse

type IResponse interface {
	// SetHeader 设置请求头
	SetHeader(key, value string) IResponse

	// Retrieve 查询单个资源的响应
	Retrieve(entity interface{})
	// TableWithPagination 表格分页响应
	TableWithPagination(resp *TableResponse)
	// ListWithPagination 分页列表的响应
	ListWithPagination(totalRow uint, entities interface{})
	// ListWithMoreFlag 查询列表的响应
	ListWithMoreFlag(hasMore bool, entities interface{})

	// Post 新增请求的响应
	Post(entity interface{})
	// Put 全量更新资源的响应
	Put(entity interface{})
	// Patch 部分更新资源的响应
	// 部分 cdn 服务商不支持 http patch 方法,如 阿里云
	Patch(entity interface{})
	// Delete 删除的响应
	Delete(err error)

	// WithMessage 通过 json 响应文本消息: {"message": "something..."}
	WithMessage(msg string)
	// WithBody 响应文本消息
	WithBody(body string)
	// WithError 响应错误消息(HttpStatus!=200)
	WithError(err error)
	// WithErrorData 响应错误消息(HttpStatus!=200),并在 header 中返回错误数据
	WithErrorData(err error, data interface{})
}

func NewResponse

func NewResponse(ctx *gin.Context, opts ...func(*response)) IResponse

NewResponse 创建 Restful 标准响应生成器

usage: 基础用法 rru := restful.NewResponse(ctx)

启用语言包

rru := restful.NewResponse(
	ctx,
	restful.WithResponseErrorMsgHandler(lang.Handle()),
)

type TableResponse

type TableResponse struct {
	TotalRow uint                          // 分页的记录条数
	Columns  []string                      // 表格列
	RowKeys  []string                      // 表格行
	Items    []*TableResponseItem          // 表格行数据
	Extends  []*TableResponseRowExtendItem // 表格行扩展数据
}

type TableResponseItem

type TableResponseItem struct {
	Column string      // 列
	RowKey string      // 行关键字
	Data   interface{} // 数据
}

type TableResponseRowExtendItem

type TableResponseRowExtendItem struct {
	RowKey string      // 行关键字
	Data   interface{} // 数据
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL