api-platform.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. 'Webkul\BagistoApi\Http\Middleware\PaginationHeaders',
  43. 'Webkul\BagistoApi\Http\Middleware\SearchMetadataResponse',
  44. 'Webkul\BagistoApi\Http\Middleware\WrapApiResponse',
  45. 'Spatie\ResponseCache\Middlewares\CacheResponse',
  46. ],
  47. ],
  48. 'resources' => [
  49. base_path('packages/Webkul/BagistoApi/src/Models/'),
  50. base_path('packages/Webkul/BagistoApi/src/Dto/ProductDetail/'),
  51. base_path('packages/Webkul/BagistoApi/src/Dto/CustomerOrder/'),
  52. base_path('packages/Webkul/BagistoApi/src/Dto/ProductSearch/'),
  53. ],
  54. 'formats' => [
  55. 'json'=> ['application/json'],
  56. ],
  57. 'patch_formats' => [
  58. 'json' => ['application/merge-patch+json'],
  59. ],
  60. 'docs_formats' => [
  61. 'jsonopenapi' => ['application/vnd.openapi+json'],
  62. 'html' => ['text/html'],
  63. ],
  64. 'error_formats' => [
  65. 'jsonproblem' => ['application/problem+json'],
  66. ],
  67. 'defaults' => [
  68. 'pagination_enabled' => true,
  69. 'pagination_partial' => false,
  70. 'pagination_client_enabled' => false,
  71. 'pagination_client_items_per_page' => true,
  72. 'pagination_client_partial' => false,
  73. 'pagination_items_per_page' => 10,
  74. 'pagination_maximum_items_per_page' => 50,
  75. 'route_prefix' => '/api',
  76. 'middleware' => [],
  77. ],
  78. 'pagination' => [
  79. 'page_parameter_name' => 'page',
  80. 'enabled_parameter_name' => 'pagination',
  81. 'items_per_page_parameter_name' => 'per_page',
  82. 'partial_parameter_name' => 'partial',
  83. ],
  84. 'graphql' => [
  85. 'enabled' => true,
  86. 'nesting_separator' => '__',
  87. 'introspection' => ['enabled' => true],
  88. 'max_query_complexity' => 400,
  89. 'max_query_depth' => 20,
  90. 'graphiql' => [
  91. 'enabled' => true,
  92. 'default_query' => null,
  93. 'default_variables' => null,
  94. ],
  95. 'graphql_playground' => [
  96. 'enabled' => true,
  97. 'default_query' => null,
  98. 'default_variables' => null,
  99. ],
  100. // GraphQL middleware for authentication and rate limiting
  101. 'middleware' => [
  102. 'Webkul\BagistoApi\Http\Middleware\SetLocaleChannel',
  103. 'Webkul\BagistoApi\Http\Middleware\VerifyGraphQLStorefrontKey',
  104. 'Webkul\BagistoApi\Http\Middleware\SearchMetadataResponse',
  105. ],
  106. ],
  107. 'graphiql' => [
  108. 'enabled' => true,
  109. ],
  110. 'name_converter' => SnakeCaseToCamelCaseNameConverter::class,
  111. 'path_segment_name_generator' => DashPathSegmentNameGenerator::class,
  112. 'exception_to_status' => [
  113. AuthenticationException::class => 401,
  114. AuthorizationException::class => 403,
  115. ValidationException::class => 400,
  116. InvalidInputException::class => 400,
  117. ],
  118. 'swagger_ui' => [
  119. 'enabled' => true,
  120. 'apiKeys' => [
  121. 'api' => [
  122. 'name' => 'Authorization',
  123. 'type' => 'header',
  124. 'scheme' => 'bearer',
  125. ],
  126. ],
  127. ],
  128. 'url_generation_strategy' => UrlGeneratorInterface::ABS_PATH,
  129. 'serializer' => [
  130. 'hydra_prefix' => false,
  131. 'datetime_format' => 'Y-m-d\TH:i:sP',
  132. ],
  133. 'cache' => 'redis',
  134. 'schema_cache' => [
  135. 'enabled' => true,
  136. 'store' => 'redis',
  137. ],
  138. 'security' => [
  139. 'sanctum' => true,
  140. ],
  141. 'rate_limit' => [
  142. 'skip_localhost' => env('RATE_LIMIT_SKIP_LOCALHOST', true),
  143. 'auth' => env('RATE_LIMIT_AUTH', 5),
  144. 'admin' => env('RATE_LIMIT_ADMIN', 60),
  145. 'shop' => env('RATE_LIMIT_SHOP', 100),
  146. 'graphql' => env('RATE_LIMIT_GRAPHQL', 100),
  147. 'cache_driver' => env('RATE_LIMIT_CACHE', 'redis'),
  148. 'cache_prefix' => 'api:rate-limit:',
  149. ],
  150. 'security_headers' => [
  151. 'enabled' => true,
  152. 'force_https' => env('APP_FORCE_HTTPS', false),
  153. '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'",
  154. ],
  155. 'api_logging' => [
  156. 'enabled' => env('API_LOG_ENABLED', true),
  157. 'log_sensitive_data' => env('API_LOG_SENSITIVE_DATA', false),
  158. 'exclude_paths' => ['docs', 'graphiql', 'swagger-ui', 'docs.json'],
  159. 'channel' => 'api',
  160. 'async' => env('API_LOG_ASYNC', true),
  161. 'queue' => env('API_LOG_QUEUE', 'api-logs'),
  162. ],
  163. 'graphql_validation' => [
  164. 'max_depth' => env('GRAPHQL_MAX_DEPTH', 10),
  165. 'max_complexity' => env('GRAPHQL_MAX_COMPLEXITY', 300),
  166. ],
  167. 'request_limits' => [
  168. 'max_size_mb' => env('MAX_REQUEST_SIZE', 10),
  169. 'max_pagination_limit' => env('MAX_PAGINATION_LIMIT', 100),
  170. ],
  171. 'database' => [
  172. 'log_queries' => env('DB_QUERY_LOG_ENABLED', false),
  173. 'slow_query_threshold' => env('DB_SLOW_QUERY_THRESHOLD', 1000),
  174. ],
  175. 'caching' => [
  176. 'enable_security_cache' => env('API_SECURITY_CACHE', true),
  177. 'security_cache_ttl' => env('API_SECURITY_CACHE_TTL', 3600),
  178. 'enable_response_cache' => env('API_RESPONSE_CACHE', true),
  179. 'response_cache_ttl' => env('API_RESPONSE_CACHE_TTL', 3600),
  180. ],
  181. 'http_cache' => [
  182. 'etag' => true,
  183. 'max_age' => 3600,
  184. 'shared_max_age' => null,
  185. 'vary' => null,
  186. 'public' => true,
  187. 'stale_while_revalidate' => 30,
  188. 'stale_if_error' => null,
  189. 'invalidation' => [
  190. 'urls' => [],
  191. 'scoped_clients' => [],
  192. 'max_header_length' => 7500,
  193. 'request_options' => [],
  194. 'purger' => ApiPlatform\HttpCache\SouinPurger::class,
  195. ],
  196. ],
  197. 'key_rotation_policy' => [
  198. 'enabled' => true,
  199. 'expiration_months' => env('API_KEY_EXPIRATION_MONTHS', 12),
  200. 'transition_days' => env('API_KEY_TRANSITION_DAYS', 7),
  201. 'cleanup_days' => env('API_KEY_CLEANUP_DAYS', 90),
  202. 'cache_ttl' => env('API_KEY_CACHE_TTL', 3600),
  203. 'storefront_key_prefix' => env('STOREFRONT_KEY_PREFIX', 'pk_storefront_'),
  204. ],
  205. ];