Javascript/웹 개발

REST API 엔드포인트 이름 규칙

Frankie 2021. 9. 2. 18:13

REST 자원 명명 가이드

 

1. resource는 단일 객체거나 집합이 될 수 있다.

 

집합 객체 URI - /customers

단일 객체 URI - /customers/{customerId}

 

2. 자원은 하위 집합 자원들을 포함한다.

 

customer - accounts(하위 집합 자원)

 

accounts 집합 자원을 얻기 위해서 URI는 - /customers/{customerId}/accounts

account 단일 자원을 얻기 위해서 URI는 - /customers/{customerId}/accounts/{accountId}

 

----------------------------------------------------------------------------------------------------

 

REST 자원 명명 모범 사례

 

1. 자원을 나타낼 때 명사 사용(단일 객체 빼고 복수 명사 사용)

 

1) document - 단일 개념

http://localhost:4000/device-management/managed-devices/{device-id}

http://localhost:4000/user-management/users/{id}

http://localhost:4000/user-management/users/admin

 

2) collection - 서버에서 관리하는 리소스 디렉토리

http://localhost:4000/device-management/managed-devices

http://localhost:4000/user-management/users

http://localhost:4000/user-management/users/{id}/accounts

 

3) store - 클라이언트에서 관리하는 리소스 디렉토리

http://localhost:4000/song-management/users/{id}/playlists

 

4) controller - 절차 개념을 모델링, 매개변수와 반환값이 있는 실행 파일과 같다.

http://localhost:4000/cart-management/users/{id}/cart/checkout

http://localhost:4000/song-management/users/{id}/playlist/play

 

2. 일관성이 중요

 

1) /를 사용해서 계층을 나타내기

http://localhost:4000/device-management

http://localhost:4000/device-management/managed-devices

http://localhost:4000/device-management/managed-devices/{id}

http://localhost:4000/device-management/managed-devices/{id}/scripts

http://localhost:4000/device-management/managed-devices/{id}/scripts/{id}

 

2) /를 사용해서 끝내지 않기

 

3) -를 사용해서 가독성 향상

http://localhost:4000/inventory-management/managed-entities/{id}/install-script-location //More readable

http://localhost:4000/inventory-management/managedEntities/{id}/installScriptLocation //Less readable

 

4) _ 사용하지 말기

 

5) 소문자 사용

 

6) 파일 확장자 사용 X

 

3. URI에 CRUD 함수 이름 사용 x

 

4. 쿼리 구성 요소를 사용해서 URI 집합 필터링

http://localhost:4000/device-management/managed-devices?region=USA  

http://localhost:4000/device-management/managed-devices?region=USA&brand=XYZ&sort=installation-date