ndbDatabase.js 5.3 KB

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