About the codec for nestjs microservice

#1

I am trying to use resgate with a nestjs microservice server backend, and want to add a new codec to work with nestjs nats message.
The resgate message is packaged as nest message data. and resgate response is got from nestjs response message.

type NestGetRequest struct {
	ID      string      `json:"id"`
	Pattern interface{} `json:"pattern,omitempty"`
	Data    GetRequest `json:"data",omitempty"`
}


func DecodeGetResponse(payload []byte) (*GetResult, error) {
	var nestR NestResponse
	err := json.Unmarshal(payload, &nestR)
	if err != nil {
		return nil, reserr.InternalError(err)
	}

	if nestR.Response == nil {
		return nil, errMissingResult
	}
	var r GetResponse
	err = json.Unmarshal(nestR.Response, &r)
	if err != nil {
		return nil, reserr.InternalError(err)
	}

	if r.Error != nil {
		return nil, r.Error
	}

	if r.Result == nil {
		return nil, errMissingResult
	}

	// Assert we got either a model or a collection
	res := r.Result
	if res.Model != nil {
		if res.Collection != nil {
			return nil, errInvalidResponse
		}
		// Assert model only has proper values
		for _, v := range res.Model {
			if v.Type != ValueTypeResource && v.Type != ValueTypePrimitive {
				return nil, errInvalidResponse
			}
		}
	} else {
		if res.Collection == nil {
			return nil, errInvalidResponse
		}
		// Assert collection only has proper values
		for _, v := range res.Collection {
			if v.Type != ValueTypeResource && v.Type != ValueTypePrimitive {
				return nil, errInvalidResponse
			}
		}
	}

	return r.Result, nil
}
#2

Welcome to the forum! :partying_face:

A creative endevour I must say :slight_smile: .
The codec.go file in the resgate repository should have all definitions of the json structures being sent to and from Resgate over NATS. So if you wish to change the encoding or those messages to match NestJS, that is the only file you should need to modify.

The codec wasn’t really meant to be “replaceable”, but that doesn’t mean you can’t do it :wink: .
Have you gotten it to work?

Best regards,
Samuel