twilio / twilio-node

Node.js helper library
MIT License
1.37k stars 495 forks source link

🐍🐪 Snake vs. camel casing, nested objects #962

Open mrienstra opened 9 months ago

mrienstra commented 9 months ago

This library converts snake_case API responses to camelCase, e.g.: https://github.com/twilio/twilio-node/blob/a23ee16/src/rest/lookups/v2/phoneNumber.ts#L218-L232

This is only occurring at the top level, but not within nested objects, e.g. lineTypeIntelligence / line_type_intelligence: https://www.twilio.com/docs/lookup/v2-api/identity-match

If the intent is to continue hardcoding these case conversions, a fix (shown only for one possible nested object) might look something like this:

    this.lineTypeIntelligence = payload.line_type_intelligence && {
      errorCode: payload.line_type_intelligence.error_code,
      mobileCountryCode: payload.line_type_intelligence.mobile_country_code,
      mobileNetworkCode: payload.line_type_intelligence.mobile_network_code,
      carrierName: payload.line_type_intelligence.carrier_name,
      type: payload.line_type_intelligence.type,
    };

or

    if (payload.line_type_intelligence) {
      const {
        error_code,
        mobile_country_code,
        mobile_network_code,
        carrier_name,
        type,
      } = payload.line_type_intelligence;
      this.lineTypeIntelligence = {
        errorCode: error_code,
        mobileCountryCode: mobile_country_code,
        mobileNetworkCode: mobile_network_code,
        carrierName: carrier_name,
        type: type,
      };
    } else {
      this.lineTypeIntelligence = payload.line_type_intelligence;
    }

Alternately, rather than hardcoding case conversions, a library (e.g. https://www.npmjs.com/package/case-anythinghttps://github.com/mesqueeb/case-anything) could be used


The docs (e.g. https://www.twilio.com/docs/lookup/v2-api/identity-match) are a little confusing, they always show snake_case, even when the selected library is "NODE.JS". I think that's a known issue, I found a mention of it from May 2018: https://github.com/twilio/twilio-node/issues/344#issuecomment-390877385

Would be nice to see if docs folks can tweak that, or at least add a note about helper libraries changing property casing. Obviously most devs will clock this right away, but still, it adds unnecessary cognitive overhead when referring to API docs.

Happy to open another issue for that, but this repo is probably not the place for that work to be tracked anyway.