Merged in feat/sw-2863-move-contentstack-router-to-trpc-package (pull request #2389)
feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
This commit is contained in:
228
packages/trpc/lib/api/endpoints.ts
Normal file
228
packages/trpc/lib/api/endpoints.ts
Normal file
@@ -0,0 +1,228 @@
|
||||
/**
|
||||
* Nested enum requires namespace
|
||||
*/
|
||||
export namespace endpoints {
|
||||
namespace base {
|
||||
export const enum path {
|
||||
availability = "availability",
|
||||
booking = "booking",
|
||||
hotel = "hotel",
|
||||
package = "package",
|
||||
profile = "profile",
|
||||
}
|
||||
|
||||
export const enum enitity {
|
||||
Ancillary = "Ancillary",
|
||||
Availabilities = "availabilities",
|
||||
Bookings = "Bookings",
|
||||
Breakfast = "breakfast",
|
||||
Cities = "Cities",
|
||||
Countries = "Countries",
|
||||
Hotels = "Hotels",
|
||||
Locations = "Locations",
|
||||
Packages = "packages",
|
||||
Profile = "Profile",
|
||||
Reward = "Reward",
|
||||
Stays = "Stays",
|
||||
Transaction = "Transaction",
|
||||
RoomFeature = "RoomFeature",
|
||||
}
|
||||
}
|
||||
|
||||
export namespace v1 {
|
||||
const version = "v1"
|
||||
/**
|
||||
* availability (Swagger)
|
||||
* https://tstapi.scandichotels.com/availability/swagger/v1/index.html
|
||||
*/
|
||||
export namespace Availability {
|
||||
export function city(cityId: string) {
|
||||
return `${base.path.availability}/${version}/${base.enitity.Availabilities}/city/${cityId}`
|
||||
}
|
||||
export function hotels() {
|
||||
return `${base.path.availability}/${version}/${base.enitity.Availabilities}/hotels`
|
||||
}
|
||||
export function hotel(hotelId: string) {
|
||||
return `${base.path.availability}/${version}/${base.enitity.Availabilities}/hotel/${hotelId}`
|
||||
}
|
||||
export function roomFeatures(hotelId: string) {
|
||||
return `${base.path.availability}/${version}/${base.enitity.RoomFeature}/features/hotel/${hotelId}`
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* booking (Swagger)
|
||||
* https://tstapi.scandichotels.com/booking/swagger/v1/index.html
|
||||
*/
|
||||
export namespace Booking {
|
||||
export const bookings = `${base.path.booking}/${version}/${base.enitity.Bookings}`
|
||||
|
||||
export function booking(confirmationNumber: string) {
|
||||
return `${bookings}/${confirmationNumber}`
|
||||
}
|
||||
export function find(confirmationNumber: string) {
|
||||
return `${bookings}/${confirmationNumber}/find`
|
||||
}
|
||||
export function cancel(confirmationNumber: string) {
|
||||
return `${bookings}/${confirmationNumber}/cancel`
|
||||
}
|
||||
export function status(confirmationNumber: string) {
|
||||
return `${bookings}/${confirmationNumber}/status`
|
||||
}
|
||||
export function priceChange(confirmationNumber: string) {
|
||||
return `${bookings}/${confirmationNumber}/priceChange`
|
||||
}
|
||||
export function packages(confirmationNumber: string) {
|
||||
return `${bookings}/${confirmationNumber}/packages`
|
||||
}
|
||||
export function guarantee(confirmationNumber: string) {
|
||||
return `${bookings}/${confirmationNumber}/guarantee`
|
||||
}
|
||||
|
||||
export const enum Stays {
|
||||
future = `${base.path.booking}/${version}/${base.enitity.Stays}/future`,
|
||||
past = `${base.path.booking}/${version}/${base.enitity.Stays}/past`,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* hotel (Swagger)
|
||||
* https://tstapi.scandichotels.com/hotel/swagger/v1/index.html
|
||||
*/
|
||||
export namespace Hotel {
|
||||
export const cities = `${base.path.hotel}/${version}/${base.enitity.Cities}`
|
||||
export namespace Cities {
|
||||
export function city(cityId: string) {
|
||||
return `${cities}/${cityId}`
|
||||
}
|
||||
export function country(countryId: string) {
|
||||
return `${cities}/country/${countryId}`
|
||||
}
|
||||
export function hotel(hotelId: string) {
|
||||
return `${cities}/hotel/${hotelId}`
|
||||
}
|
||||
}
|
||||
|
||||
export const countries = `${base.path.hotel}/${version}/${base.enitity.Countries}`
|
||||
export namespace Countries {
|
||||
export function country(countryId: string) {
|
||||
return `${countries}/${countryId}`
|
||||
}
|
||||
}
|
||||
|
||||
export const hotels = `${base.path.hotel}/${version}/${base.enitity.Hotels}`
|
||||
export namespace Hotels {
|
||||
export function hotel(hotelId: string) {
|
||||
return `${hotels}/${hotelId}`
|
||||
}
|
||||
export function meetingRooms(hotelId: string) {
|
||||
return `${hotels}/${hotelId}/meetingRooms`
|
||||
}
|
||||
export function merchantInformation(hotelId: string) {
|
||||
return `${hotels}/${hotelId}/merchantInformation`
|
||||
}
|
||||
export function nearbyHotels(hotelId: string) {
|
||||
return `${hotels}/${hotelId}/nearbyHotels`
|
||||
}
|
||||
export function restaurants(hotelId: string) {
|
||||
return `${hotels}/${hotelId}/restaurants`
|
||||
}
|
||||
export function roomCategories(hotelId: string) {
|
||||
return `${hotels}/${hotelId}/roomCategories`
|
||||
}
|
||||
export function additionalData(hotelId: string) {
|
||||
return `${hotels}/${hotelId}/additionalData`
|
||||
}
|
||||
}
|
||||
|
||||
export const locations = `${base.path.hotel}/${version}/${base.enitity.Locations}`
|
||||
}
|
||||
|
||||
/**
|
||||
* package (Swagger)
|
||||
* https://tstapi.scandichotels.com/package/swagger/v1/index.html
|
||||
*/
|
||||
export namespace Package {
|
||||
export namespace Ancillary {
|
||||
export function hotel(hotelId: string) {
|
||||
return `${base.path.package}/${version}/${base.enitity.Ancillary}/hotel/${hotelId}`
|
||||
}
|
||||
export function hotelAncillaries(hotelId: string) {
|
||||
return `${base.path.package}/${version}/${base.enitity.Ancillary}/hotel/${hotelId}/ancillaries`
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Breakfast {
|
||||
export function hotel(hotelId: string) {
|
||||
return `${base.path.package}/${version}/${base.enitity.Breakfast}/hotel/${hotelId}`
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Packages {
|
||||
export function hotel(hotelId: string) {
|
||||
return `${base.path.package}/${version}/${base.enitity.Packages}/hotel/${hotelId}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* profile (Swagger)
|
||||
* https://tstapi.scandichotels.com/profile/swagger/v1/index.html
|
||||
*/
|
||||
export namespace Profile {
|
||||
export const invalidateSessions = `${base.path.profile}/${version}/${base.enitity.Profile}/invalidateSessions`
|
||||
export const membership = `${base.path.profile}/${version}/${base.enitity.Profile}/membership`
|
||||
export const profile = `${base.path.profile}/${version}/${base.enitity.Profile}`
|
||||
export const subscriberId = `${base.path.profile}/${version}/${base.enitity.Profile}/SubscriberId`
|
||||
export const link = `${base.path.profile}/${version}/${base.enitity.Profile}/link`
|
||||
export const unlink = `${base.path.profile}/${version}/${base.enitity.Profile}/Unlink`
|
||||
export const matchTier = `${base.path.profile}/${version}/${base.enitity.Profile}/MatchTier`
|
||||
export const pointTransfer = `${base.path.profile}/${version}/${base.enitity.Profile}/PointTransfer/Partner`
|
||||
|
||||
export function deleteProfile(profileId: string) {
|
||||
return `${profile}/${profileId}`
|
||||
}
|
||||
|
||||
export const creditCards = `${base.path.profile}/${version}/${base.enitity.Profile}/creditCards`
|
||||
export namespace CreditCards {
|
||||
export const initiateSaveCard = `${creditCards}/initiateSaveCard`
|
||||
|
||||
export function deleteCreditCard(creditCardId: string) {
|
||||
return `${creditCards}/${creditCardId}`
|
||||
}
|
||||
export function transaction(transactionId: string) {
|
||||
return `${creditCards}/${transactionId}`
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Reward {
|
||||
export const allTiers = `${base.path.profile}/${version}/${base.enitity.Reward}/allTiers`
|
||||
export const reward = `${base.path.profile}/${version}/${base.enitity.Reward}`
|
||||
export const redeem = `${base.path.profile}/${version}/${base.enitity.Reward}/redeem`
|
||||
export const unwrap = `${base.path.profile}/${version}/${base.enitity.Reward}/unwrap`
|
||||
|
||||
export function claim(rewardId: string) {
|
||||
return `${base.path.profile}/${version}/${base.enitity.Reward}/Claim/${rewardId}`
|
||||
}
|
||||
}
|
||||
|
||||
export const enum Transaction {
|
||||
friendTransactions = `${base.path.profile}/${version}/${base.enitity.Transaction}/friendTransactions`,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export namespace v2 {
|
||||
const version = "v2"
|
||||
|
||||
/**
|
||||
* profile (Swagger)
|
||||
* https://tstapi.scandichotels.com/profile/swagger/v2/index.html
|
||||
*/
|
||||
export namespace Profile {
|
||||
export const profile = `${base.path.profile}/${version}/${base.enitity.Profile}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type Endpoint = string
|
||||
123
packages/trpc/lib/api/index.ts
Normal file
123
packages/trpc/lib/api/index.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
import merge from "deepmerge"
|
||||
import fetchRetry from "fetch-retry"
|
||||
|
||||
import { env } from "../../env/server"
|
||||
|
||||
import type { Endpoint } from "./endpoints"
|
||||
|
||||
export { endpoints } from "./endpoints"
|
||||
|
||||
interface RequestOptionsWithJSONBody
|
||||
extends Omit<RequestInit, "body" | "method"> {
|
||||
body?: Record<string, unknown>
|
||||
}
|
||||
|
||||
interface RequestOptionsWithOutBody
|
||||
extends Omit<RequestInit, "body" | "method"> {}
|
||||
|
||||
const defaultOptions: RequestInit = {
|
||||
cache: "no-store",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
mode: "cors",
|
||||
}
|
||||
|
||||
const wrappedFetch = fetchRetry(fetch, {
|
||||
retries: 2,
|
||||
retryDelay: function (attempt) {
|
||||
return Math.pow(2, attempt) * 150 // 150, 300, 600
|
||||
},
|
||||
})
|
||||
|
||||
export async function get(
|
||||
endpoint: Endpoint,
|
||||
options: RequestOptionsWithOutBody,
|
||||
params = {}
|
||||
) {
|
||||
const url = new URL(env.API_BASEURL)
|
||||
url.pathname = endpoint
|
||||
url.search = new URLSearchParams(params).toString()
|
||||
|
||||
return wrappedFetch(url, {
|
||||
...merge.all([defaultOptions, { method: "GET" }, options]),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
}
|
||||
|
||||
export async function patch(
|
||||
endpoint: Endpoint | `${Endpoint}/${string}`,
|
||||
options: RequestOptionsWithJSONBody,
|
||||
params = {}
|
||||
) {
|
||||
const { body, ...requestOptions } = options
|
||||
const url = new URL(env.API_BASEURL)
|
||||
url.pathname = endpoint
|
||||
url.search = new URLSearchParams(params).toString()
|
||||
return wrappedFetch(url, {
|
||||
...merge.all([
|
||||
defaultOptions,
|
||||
{ body: JSON.stringify(body), method: "PATCH" },
|
||||
requestOptions,
|
||||
]),
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
})
|
||||
}
|
||||
|
||||
export async function post(
|
||||
endpoint: Endpoint | `${Endpoint}/${string}`,
|
||||
options: RequestOptionsWithJSONBody,
|
||||
params = {}
|
||||
) {
|
||||
const { body, ...requestOptions } = options
|
||||
const url = new URL(env.API_BASEURL)
|
||||
url.pathname = endpoint
|
||||
url.search = new URLSearchParams(params).toString()
|
||||
return wrappedFetch(url, {
|
||||
...merge.all([
|
||||
defaultOptions,
|
||||
{ body: JSON.stringify(body), method: "POST" },
|
||||
requestOptions,
|
||||
]),
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
})
|
||||
}
|
||||
|
||||
export async function put(
|
||||
endpoint: Endpoint | `${Endpoint}/${string}`,
|
||||
options: RequestOptionsWithJSONBody,
|
||||
params = {}
|
||||
) {
|
||||
const { body, ...requestOptions } = options
|
||||
const url = new URL(env.API_BASEURL)
|
||||
url.pathname = endpoint
|
||||
url.search = new URLSearchParams(params).toString()
|
||||
return wrappedFetch(url, {
|
||||
...merge.all([
|
||||
defaultOptions,
|
||||
{ body: JSON.stringify(body), method: "PUT" },
|
||||
requestOptions,
|
||||
]),
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
})
|
||||
}
|
||||
|
||||
export async function remove(
|
||||
endpoint: Endpoint | `${Endpoint}/${string}`,
|
||||
options: RequestOptionsWithJSONBody,
|
||||
params = {}
|
||||
) {
|
||||
const { body, ...requestOptions } = options
|
||||
const url = new URL(env.API_BASEURL)
|
||||
url.pathname = endpoint
|
||||
url.search = new URLSearchParams(params).toString()
|
||||
return wrappedFetch(url, {
|
||||
...merge.all([
|
||||
defaultOptions,
|
||||
{ body: JSON.stringify(body), method: "DELETE" },
|
||||
requestOptions,
|
||||
]),
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user