feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.08k stars 752 forks source link

How do I format the content.result? #1157

Closed feathers-wing closed 5 years ago

feathers-wing commented 5 years ago

I need to return an XML format in content.result , and to set the content-type, but no method is found

perzanko commented 5 years ago

As far as I know, there is no built-in method for changing content-type and converting the result to XML. You can simply implement it as the 'after' hook.

ktfth commented 5 years ago

You can utilize something like that on the application for correctly configuration: https://www.npmjs.com/package/body-parser-xml

daffl commented 5 years ago

How to format the response is documented in the Express transport chapter. You can either use a global REST formatter or a service specific middleware.

feathers-wing commented 5 years ago

thank you very much!!! 🌹🌹 i have a try!

e-kibet commented 1 week ago

Hello @daffl

I have noticed something. Let's take an example where I have the following hook:

// src/hooks/standardizeResponse.ts

import { HookContext } from '@feathersjs/feathers';

export const standardizeResponse = (context: HookContext) => {
    const { result, error } = context;

    if (error) {
        context.result = {
            status: 'error',
            message: error.message || 'An error occurred',
            data: null,
            meta: {
                errorCode: error.code || 'UNKNOWN',
                timestamp: new Date().toISOString()
            }
        };
    } else if (result) {
        context.result = {
            status: 'success',
            message: 'Request was successful',
            data: result,
            meta: {
                timestamp: new Date().toISOString()
            }
        };
    }

    return context;
};

which i intend to apply on the let say user service on:

after: {
      all: [standardizeResponse]
    },
    error: {
      all: [standardizeResponse]
    }

/authetication is failing since it relies on the LocalStrategy.findEntity which does not expect the format given, I have tried to apply global on the global services on app.ts but still authetication api is failing. Kindly assist

daffl commented 1 week ago

You have to customize the strategy accordingly. Also note that your error handler will not return a proper error but instead turn it into a normal response. This might cause various issues when an actual error (and HTTP error code) is expected. This should probably still be context.error = {}.

e-kibet commented 1 week ago

Hey @daffl thanks. I managed to implement the customize the strategy but the issue is with the authetication on getEntityQuery on authentication-local when the users response output is returned as a different format

daffl commented 1 week ago

You might have to re-implement the entire findEntity and getEntity to work with your format.