|
1 | | -# @lfg/query-coder |
| 1 | +## @lfg/query-coder |
| 2 | +URL query coder/decoder with configurable user pattern. It provides the most comfortable experience for encoding and decoding complex nested filter objects. [Check out example for more info](#example). |
2 | 3 |
|
3 | | -#### URL query coder/decoder |
| 4 | + |
| 5 | +### Installation |
| 6 | +```zsh |
| 7 | +yarn add @lfg/query-coder |
| 8 | +``` |
| 9 | + |
| 10 | +### Basic Example |
| 11 | +Imaging having a such interface for your querying filters: |
| 12 | +```ts |
| 13 | +type SearchGroupsFilter = { |
| 14 | + gameId?: 'WorldOfWarcraft' | 'WildRift' | 'LostArk'; |
| 15 | + gameMode?: GameMode; |
| 16 | + from?: Date; |
| 17 | + language?: Language; |
| 18 | + |
| 19 | + lostArk?: LostArkFilterInput; |
| 20 | + wildRift?: WildRiftFilterInput; |
| 21 | + wow?: WowFilterInput; |
| 22 | +}; |
| 23 | +``` |
| 24 | + |
| 25 | +Let's take 1 example of this: |
| 26 | +```ts |
| 27 | +const filters: SearchGroupsFilter = { |
| 28 | + gameId: "WorldOfWarcraft", |
| 29 | + gameMode: GameMode.WowMythicPlus, |
| 30 | + language: Language.En, |
| 31 | + wow: { |
| 32 | + faction: WowFaction.Alliance, |
| 33 | + dungeon: WowDungeon.MistsOfTirnaScithe, |
| 34 | + minRioRating: 1400, |
| 35 | + region: WowRegion.Europe, |
| 36 | + }, |
| 37 | +}; |
| 38 | + |
| 39 | +``` |
| 40 | +Which should result in `const query = "game=wow&mode=mplus&language=en&faction=alliance&dungeon=mots&rating=1400®ion=eu"`. |
| 41 | +So we want to: |
| 42 | +* flatten nested filter object in a query string |
| 43 | +* rename certain keys (`gameMode` -> `mode`) |
| 44 | +* re-map certain values (`WorldOfWarcraft` -> `wow`) |
| 45 | + |
| 46 | +With this lib, you can init `QueryCoder` and that's it! 🎉 |
| 47 | +```ts |
| 48 | +import { QueryCoder, QueryHandler } from '@lfg/query-coder'; |
| 49 | + |
| 50 | +// passing generic interface checks, whether node keys |
| 51 | +// are the same as in provided interface |
| 52 | +const query = new QueryCoder<SearchGroupsFilter>({ |
| 53 | + gameId: new QueryHandler({ |
| 54 | + query: "game", |
| 55 | + aliases: { |
| 56 | + WorldOfWarcraft: "wow", |
| 57 | + WildRift: "wr", |
| 58 | + LostArk: "la", |
| 59 | + }, |
| 60 | + }), |
| 61 | + gameMode: new QueryHandler({ query: "mode" }), |
| 62 | + language: new QueryHandler({ query: "language" }), |
| 63 | + wow: { |
| 64 | + faction: new QueryHandler({ |
| 65 | + query: "faction", |
| 66 | + /** |
| 67 | + * decodeCondition is Partial of generic |
| 68 | + * if decodeCondition is set, search query would be handled |
| 69 | + * with this handler only if decodeCondition matches query |
| 70 | + * For more info, check out section below |
| 71 | + */ |
| 72 | + decodeCondition: { gameId: "WorldOfWarcraft" }, |
| 73 | + }), |
| 74 | + dungeon: new QueryHandler({ |
| 75 | + query: "dungeon", |
| 76 | + decodeCondition: { gameId: "WorldOfWarcraft" }, |
| 77 | + }), |
| 78 | + minRioRating: new QueryHandler({ |
| 79 | + query: "minRioRating", |
| 80 | + decodeCondition: { gameId: "WorldOfWarcraft" }, |
| 81 | + /** |
| 82 | + * You should provide a primitive type, which is different from string |
| 83 | + * Otherwise, url query "foo=313" can result in an unexpected result |
| 84 | + */ |
| 85 | + type: Type.Number, |
| 86 | + }), |
| 87 | + region: new QueryHandler({ |
| 88 | + query: "region", |
| 89 | + decodeCondition: { gameId: "WorldOfWarcraft" }, |
| 90 | + }), |
| 91 | + }, |
| 92 | +}); |
| 93 | + |
| 94 | +query.encode(filters); // should result in query variable |
| 95 | +query.decode(query); // should result in filters variable |
| 96 | +``` |
0 commit comments