api-platform.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. use ApiPlatform\Metadata\Operation\DashPathSegmentNameGenerator;
  12. use ApiPlatform\Metadata\UrlGeneratorInterface;
  13. use Illuminate\Auth\Access\AuthorizationException;
  14. use Illuminate\Auth\AuthenticationException;
  15. use Symfony\Component\Serializer\NameConverter\SnakeCaseToCamelCaseNameConverter;
  16. use Webkul\BagistoApi\Exception\InvalidInputException;
  17. use Webkul\BagistoApi\Exception\ValidationException;
  18. return [
  19. 'title' => '',
  20. 'description' => '',
  21. 'version' => '1.0.0',
  22. 'show_webby' => true,
  23. 'routes' => [
  24. 'domain' => null,
  25. // Global middleware applied to every API Platform routes
  26. // HandleInvalidInputException: Catches validation errors and returns RFC 7807 format
  27. // VerifyStorefrontKey: Validates X-STOREFRONT-KEY header and rate limiting for shop APIs
  28. // BagistoApiDocumentationMiddleware: Handles custom /api index and documentation pages
  29. // ForceApiJson: Ensures API responses have JSON content-type
  30. // CacheResponse: Using custom ApiAwareResponseCache profile that:
  31. // - Excludes API routes from caching (APIs need fresh data)
  32. // - Caches shop pages for performance
  33. // - Only caches HTML, not JSON responses
  34. 'middleware' => [
  35. 'Webkul\BagistoApi\Http\Middleware\HandleInvalidInputException',
  36. 'Webkul\BagistoApi\Http\Middleware\SecurityHeaders',
  37. 'Webkul\BagistoApi\Http\Middleware\LogApiRequests',
  38. 'Webkul\BagistoApi\Http\Middleware\SetLocaleChannel',
  39. 'Webkul\BagistoApi\Http\Middleware\VerifyStorefrontKey',
  40. 'Webkul\BagistoApi\Http\Middleware\BagistoApiDocumentationMiddleware',
  41. 'Webkul\BagistoApi\Http\Middleware\ForceApiJson',
  42. 'Spatie\ResponseCache\Middlewares\CacheResponse',
  43. ],
  44. ],
  45. 'resources' => [
  46. base_path('packages/Webkul/BagistoApi/src/Models/'),
  47. ],
  48. 'formats' => [
  49. 'json'=> ['application/json'],
  50. ],
  51. 'patch_formats' => [
  52. 'json' => ['application/merge-patch+json'],
  53. ],
  54. 'docs_formats' => [
  55. 'jsonopenapi' => ['application/vnd.openapi+json'],
  56. 'html' => ['text/html'],
  57. ],
  58. 'error_formats' => [
  59. 'jsonproblem' => ['application/problem+json'],
  60. ],
  61. 'defaults' => [
  62. 'pagination_enabled' => true,
  63. 'pagination_partial' => false,
  64. 'pagination_client_enabled' => false,
  65. 'pagination_client_items_per_page' => false,
  66. 'pagination_client_partial' => false,
  67. 'pagination_items_per_page' => 10,
  68. 'pagination_maximum_items_per_page' => 50,
  69. 'route_prefix' => '/api',
  70. 'middleware' => [],
  71. ],
  72. 'pagination' => [
  73. 'page_parameter_name' => 'page',
  74. 'enabled_parameter_name' => 'pagination',
  75. 'items_per_page_parameter_name' => 'itemsPerPage',
  76. 'partial_parameter_name' => 'partial',
  77. ],
  78. 'graphql' => [
  79. 'enabled' => true,
  80. 'nesting_separator' => '__',
  81. 'introspection' => ['enabled' => true],
  82. 'max_query_complexity' => 400,
  83. 'max_query_depth' => 20,
  84. 'graphiql' => [
  85. 'enabled' => true,
  86. 'default_query' => null,
  87. 'default_variables' => null,
  88. ],
  89. 'graphql_playground' => [
  90. 'enabled' => true,
  91. 'default_query' => null,
  92. 'default_variables' => null,
  93. ],
  94. // GraphQL middleware for authentication and rate limiting
  95. 'middleware' => [
  96. 'Webkul\BagistoApi\Http\Middleware\SetLocaleChannel',
  97. 'Webkul\BagistoApi\Http\Middleware\VerifyGraphQLStorefrontKey',
  98. ],
  99. ],
  100. 'graphiql' => [
  101. 'enabled' => true,
  102. ],
  103. 'name_converter' => SnakeCaseToCamelCaseNameConverter::class,
  104. 'path_segment_name_generator' => DashPathSegmentNameGenerator::class,
  105. 'exception_to_status' => [
  106. AuthenticationException::class => 401,
  107. AuthorizationException::class => 403,
  108. ValidationException::class => 400,
  109. InvalidInputException::class => 400,
  110. ],
  111. 'swagger_ui' => [
  112. 'enabled' => true,
  113. 'apiKeys' => [
  114. 'api' => [
  115. 'name' => 'Authorization',
  116. 'type' => 'header',
  117. 'scheme' => 'bearer',
  118. ],
  119. ],
  120. ],
  121. 'url_generation_strategy' => UrlGeneratorInterface::ABS_PATH,
  122. 'serializer' => [
  123. 'hydra_prefix' => false,
  124. 'datetime_format' => 'Y-m-d\TH:i:sP',
  125. ],
  126. 'cache' => 'redis',
  127. 'schema_cache' => [
  128. 'enabled' => true,
  129. 'store' => 'redis',
  130. ],
  131. 'security' => [
  132. 'sanctum' => true,
  133. ],
  134. 'rate_limit' => [
  135. 'skip_localhost' => env('RATE_LIMIT_SKIP_LOCALHOST', true),
  136. 'auth' => env('RATE_LIMIT_AUTH', 5),
  137. 'admin' => env('RATE_LIMIT_ADMIN', 60),
  138. 'shop' => env('RATE_LIMIT_SHOP', 100),
  139. 'graphql' => env('RATE_LIMIT_GRAPHQL', 100),
  140. 'cache_driver' => env('RATE_LIMIT_CACHE', 'redis'),
  141. 'cache_prefix' => 'api:rate-limit:',
  142. ],
  143. 'security_headers' => [
  144. 'enabled' => true,
  145. 'force_https' => env('APP_FORCE_HTTPS', false),
  146. 'csp_header' => "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'",
  147. ],
  148. 'api_logging' => [
  149. 'enabled' => env('API_LOG_ENABLED', true),
  150. 'log_sensitive_data' => env('API_LOG_SENSITIVE_DATA', false),
  151. 'exclude_paths' => ['docs', 'graphiql', 'swagger-ui', 'docs.json'],
  152. 'channel' => 'api',
  153. 'async' => env('API_LOG_ASYNC', true),
  154. 'queue' => env('API_LOG_QUEUE', 'api-logs'),
  155. ],
  156. 'graphql_validation' => [
  157. 'max_depth' => env('GRAPHQL_MAX_DEPTH', 10),
  158. 'max_complexity' => env('GRAPHQL_MAX_COMPLEXITY', 300),
  159. ],
  160. 'request_limits' => [
  161. 'max_size_mb' => env('MAX_REQUEST_SIZE', 10),
  162. 'max_pagination_limit' => env('MAX_PAGINATION_LIMIT', 100),
  163. ],
  164. 'database' => [
  165. 'log_queries' => env('DB_QUERY_LOG_ENABLED', false),
  166. 'slow_query_threshold' => env('DB_SLOW_QUERY_THRESHOLD', 1000),
  167. ],
  168. 'caching' => [
  169. 'enable_security_cache' => env('API_SECURITY_CACHE', true),
  170. 'security_cache_ttl' => env('API_SECURITY_CACHE_TTL', 3600),
  171. 'enable_response_cache' => env('API_RESPONSE_CACHE', true),
  172. 'response_cache_ttl' => env('API_RESPONSE_CACHE_TTL', 3600),
  173. ],
  174. 'http_cache' => [
  175. 'etag' => true,
  176. 'max_age' => 3600,
  177. 'shared_max_age' => null,
  178. 'vary' => null,
  179. 'public' => true,
  180. 'stale_while_revalidate' => 30,
  181. 'stale_if_error' => null,
  182. 'invalidation' => [
  183. 'urls' => [],
  184. 'scoped_clients' => [],
  185. 'max_header_length' => 7500,
  186. 'request_options' => [],
  187. 'purger' => ApiPlatform\HttpCache\SouinPurger::class,
  188. ],
  189. ],
  190. 'key_rotation_policy' => [
  191. 'enabled' => true,
  192. 'expiration_months' => env('API_KEY_EXPIRATION_MONTHS', 12),
  193. 'transition_days' => env('API_KEY_TRANSITION_DAYS', 7),
  194. 'cleanup_days' => env('API_KEY_CLEANUP_DAYS', 90),
  195. 'cache_ttl' => env('API_KEY_CACHE_TTL', 3600),
  196. 'storefront_key_prefix' => env('STOREFRONT_KEY_PREFIX', 'pk_storefront_'),
  197. ],
  198. ];