api-fake-rest.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /// <reference types="cypress" />
  2. /// <reference types="../../index.d.ts" />
  3. import { getIdFromURL } from "../../../utils";
  4. const hostname = "api.fake-rest.refine.dev";
  5. Cypress.Commands.add("interceptGETPosts", () => {
  6. return cy
  7. .intercept(
  8. {
  9. method: "GET",
  10. hostname: hostname,
  11. pathname: "/posts",
  12. },
  13. {
  14. fixture: "posts.json",
  15. },
  16. )
  17. .as("getPosts");
  18. });
  19. Cypress.Commands.add("interceptGETPost", () => {
  20. return cy
  21. .fixture("posts")
  22. .then((posts) => {
  23. return cy.intercept(
  24. {
  25. method: "GET",
  26. hostname: hostname,
  27. pathname: "/posts/*",
  28. },
  29. (req) => {
  30. const id = getIdFromURL(req.url);
  31. const post = posts.find((post) => post.id === id);
  32. if (!post) {
  33. req.reply(404, {});
  34. return;
  35. }
  36. req.reply(post);
  37. },
  38. );
  39. })
  40. .as("getPost");
  41. });
  42. Cypress.Commands.add("interceptPOSTPost", () => {
  43. return cy.fixture("posts").then((posts) =>
  44. cy
  45. .intercept(
  46. {
  47. method: "POST",
  48. hostname: hostname,
  49. pathname: "/posts",
  50. },
  51. (req) => {
  52. const merged = Object.assign({}, req.body, {
  53. id: posts.length + 1,
  54. });
  55. return req.reply(merged);
  56. },
  57. )
  58. .as("postPost"),
  59. );
  60. });
  61. Cypress.Commands.add("interceptPATCHPost", () => {
  62. return cy
  63. .fixture("posts")
  64. .then((posts) => {
  65. return cy.intercept(
  66. {
  67. method: "PATCH",
  68. hostname: hostname,
  69. pathname: "/posts/*",
  70. },
  71. (req) => {
  72. const id = getIdFromURL(req.url);
  73. const post = posts.find((post) => post.id === id);
  74. if (!post) {
  75. return req.reply(404, {});
  76. }
  77. const merged = Object.assign({}, post, req.body);
  78. return req.reply(merged);
  79. },
  80. );
  81. })
  82. .as("patchPost");
  83. });
  84. Cypress.Commands.add("interceptDELETEPost", () => {
  85. return cy
  86. .intercept(
  87. {
  88. method: "DELETE",
  89. hostname: hostname,
  90. pathname: "/posts/*",
  91. },
  92. {},
  93. )
  94. .as("deletePost");
  95. });
  96. Cypress.Commands.add("interceptGETCategories", () => {
  97. return cy
  98. .intercept(
  99. {
  100. method: "GET",
  101. hostname: hostname,
  102. pathname: "/categories",
  103. },
  104. { fixture: "categories.json" },
  105. )
  106. .as("getCategories");
  107. });
  108. Cypress.Commands.add("interceptGETCategory", () => {
  109. return cy
  110. .fixture("categories")
  111. .then((categories) => {
  112. return cy.intercept(
  113. {
  114. method: "GET",
  115. hostname: hostname,
  116. pathname: "/categories/*",
  117. },
  118. (req) => {
  119. const id = getIdFromURL(req.url);
  120. const category = categories.find(
  121. (category) => category.id.toString() === id.toString(),
  122. );
  123. if (!category) {
  124. req.reply(404, {});
  125. return;
  126. }
  127. req.reply(category);
  128. },
  129. );
  130. })
  131. .as("getCategory");
  132. });
  133. Cypress.Commands.add("interceptGETBlogPosts", () => {
  134. return cy
  135. .intercept(
  136. {
  137. method: "GET",
  138. hostname: hostname,
  139. pathname: "/blog_posts",
  140. },
  141. {
  142. fixture: "blog-posts.json",
  143. },
  144. )
  145. .as("getBlogPosts");
  146. });
  147. Cypress.Commands.add("interceptGETBlogPost", () => {
  148. return cy
  149. .fixture("blog-posts")
  150. .then((posts) => {
  151. return cy.intercept(
  152. {
  153. method: "GET",
  154. hostname: hostname,
  155. pathname: "/blog_posts/*",
  156. },
  157. (req) => {
  158. const id = getIdFromURL(req.url);
  159. const post = posts.find((post) => post.id === id);
  160. if (!post) {
  161. req.reply(404, {});
  162. return;
  163. }
  164. req.reply(post);
  165. },
  166. );
  167. })
  168. .as("getBlogPost");
  169. });
  170. Cypress.Commands.add("interceptPOSTBlogPost", () => {
  171. return cy.fixture("blog-posts").then((posts) =>
  172. cy
  173. .intercept(
  174. {
  175. method: "POST",
  176. hostname: hostname,
  177. pathname: "/blog_posts",
  178. },
  179. (req) => {
  180. const merged = Object.assign({}, req.body, {
  181. id: posts.length + 1,
  182. });
  183. return req.reply(merged);
  184. },
  185. )
  186. .as("postBlogPost"),
  187. );
  188. });
  189. Cypress.Commands.add("interceptPATCHBlogPost", () => {
  190. return cy
  191. .fixture("blog-posts")
  192. .then((posts) => {
  193. return cy.intercept(
  194. {
  195. method: "PATCH",
  196. hostname: hostname,
  197. pathname: "/blog_posts/*",
  198. },
  199. (req) => {
  200. const id = getIdFromURL(req.url);
  201. const post = posts.find((post) => post.id === id);
  202. if (!post) {
  203. return req.reply(404, {});
  204. }
  205. const merged = Object.assign({}, post, req.body);
  206. return req.reply(merged);
  207. },
  208. );
  209. })
  210. .as("patchBlogPost");
  211. });
  212. Cypress.Commands.add("interceptDELETEBlogPost", () => {
  213. return cy
  214. .intercept(
  215. {
  216. method: "DELETE",
  217. hostname: hostname,
  218. pathname: "/blog_posts/*",
  219. },
  220. {},
  221. )
  222. .as("deleteBlogPost");
  223. });