ndbDatabase.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Provides interface for ndb database.
  3. *
  4. * See the <a href="https://ndb.nal.usda.gov/ndb/doc/apilist/API-FOOD-REPORTV2.md">
  5. * official documentation</a> for details.
  6. */
  7. (function() {
  8. // Methods description for ndb APIs that return a list.
  9. var listMethod = {
  10. get: {
  11. method: "GET",
  12. isArray: true,
  13. transformRequest: function(data, headers) {
  14. return angular.extend({}, headers, {'Content-Type': 'application/json'});
  15. },
  16. headers: {
  17. 'Content-Type': 'application/json'
  18. },
  19. transformResponse: function(data, headersGetter, status) {
  20. var json = angular.fromJson(data);
  21. return status < 400 && json.list? json.list.item : json.errors.error;
  22. }
  23. }
  24. };
  25. // Methods description for ndb APIs that return a report.
  26. var reportMethod = {
  27. get: {
  28. method: "GET",
  29. transformRequest: function(data, headers) {
  30. return angular.extend({}, headers, {
  31. 'Content-Type': 'application/json'
  32. });
  33. },
  34. headers: {
  35. 'Content-Type': 'application/json'
  36. },
  37. transformResponse: function(data, headersGetter, status) {
  38. var json = angular.fromJson(data);
  39. return status < 400 && json.report.food? json.report.food : json.errors.error;
  40. }
  41. }
  42. };
  43. angular.module('ndbDatabase', ['ngResource'])
  44. /*
  45. * Query the usda ndb database for a list of elements by keyword.
  46. *
  47. * See <a href="https://ndb.nal.usda.gov/ndb/doc/apilist/API-SEARCH.md">official reference</a> for details.
  48. *
  49. * @param key USDA NDB api key.
  50. * @param query Keywords to search
  51. * @param fg Food group ID
  52. * @param max Maximum number of items to return
  53. * @param offset Beginning item in the result set
  54. * @param sort Either n=name or id. (id varies by type: nutrient number for nutrients, NDBno for foods, food group id for food groups)
  55. * @returns A list of ndb items matching the query.
  56. */
  57. .factory('NDBSearch', ['$resource', function($resource) {
  58. return $resource('https://api.nal.usda.gov/ndb/search?'+
  59. 'q=:query&ds=Standard+Reference&api_key=:key&fg=:groups&max=:max&offset=:offset', {}, listMethod);
  60. }])
  61. /*
  62. * Query the usda ndb database for a list of elements.
  63. *
  64. * See <a href="https://ndb.nal.usda.gov/ndb/doc/apilist/API-LIST.md">official reference</a> for details.
  65. *
  66. * @param key USDA NDB api key.
  67. * @param type One of d = derivation codes, f = food , n = all nutrients, ns = speciality nutrients, nr = standard release nutrients,g = food group
  68. * @param max Maximum number of items to return
  69. * @param offset Beginning item in the result set
  70. * @param sort Either n=name or id. (id varies by type: nutrient number for nutrients, NDBno for foods, food group id for food groups)
  71. * @returns A list of ndb items matching the type.
  72. */
  73. .factory('NDBList', ['$resource', function($resource) {
  74. return $resource('https://api.nal.usda.gov/ndb/list?' +
  75. 'api_key=:key&lt=:type&sort=:sort&max=:max&offset=:offset', {}, listMethod);
  76. }])
  77. /*
  78. * Gets a detailed list of information about a food.
  79. *
  80. * See <a href="https://ndb.nal.usda.gov/ndb/doc/apilist/API-FOOD-REPORT.md">official reference</a> for details.
  81. *
  82. * @param key USDA NDB api key.
  83. * @param ndbno Food id number
  84. * @param type One of [b]asic or [f]ull or [s]tats
  85. * @returns A report matching the ndb item.
  86. */
  87. .factory('NDBFood', ['$resource', function($resource) {
  88. return $resource('https://api.nal.usda.gov/ndb/reports/?api_key=:key&ndbno=:ndbno&type=:type', {}, reportMethod);
  89. }])
  90. /*
  91. * Gets a detailed list of information about the nutrients in a food.
  92. *
  93. * See <a href="https://ndb.nal.usda.gov/ndb/doc/apilist/API-NUTRIENT-REPORT.md">official reference</a> for details.
  94. *
  95. * @param key USDA NDB api key.
  96. * @param groups Food group ID
  97. * @param max Maximum number of items to return
  98. * @param offset Beginning item in the result set
  99. * @param nums The food to look up
  100. * @param nutrients A list of up to 20 nutrient_id's to include
  101. * @param sort Either n=name or id. (id varies by type: nutrient number for nutrients, NDBno for foods, food group id for food groups)
  102. * @returns A list of ndb items matching the query.
  103. */
  104. .factory('NDBNutrients', ['$resource', function($resource) {
  105. return $resource('https://api.nal.usda.gov/ndb/nutrients?'+
  106. 'api_key=:key&nbno=:nums&nutrients=:nutrients&fg=:groups&max=:max&offset=:offset&sort=:sort', {}, listMethod);
  107. }]);
  108. })();