@boon4681/giri 0.0.3-alpha-4 → 0.0.3-alpha-6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -151,6 +151,15 @@ function buildObjectSchema(type, ctx) {
151
151
  }
152
152
  return schema;
153
153
  }
154
+ function toJsonReturnType(type, ctx) {
155
+ const symbol = ctx.checker.getPropertyOfType(type, "toJSON");
156
+ if (!symbol) {
157
+ return void 0;
158
+ }
159
+ const methodType = ctx.checker.getTypeOfSymbolAtLocation(symbol, ctx.location);
160
+ const [signature] = methodType.getCallSignatures();
161
+ return signature ? ctx.checker.getReturnTypeOfSignature(signature) : void 0;
162
+ }
154
163
  function defName(type) {
155
164
  const symbol = type.getSymbol() ?? type.aliasSymbol;
156
165
  const name = symbol?.getName();
@@ -164,6 +173,12 @@ function walkObject(type, ctx) {
164
173
  if (isDateType(type)) {
165
174
  return { type: "string", format: "date-time" };
166
175
  }
176
+ if (!checker.isArrayType(type) && !checker.isTupleType(type)) {
177
+ const jsonReturn = toJsonReturnType(type, ctx);
178
+ if (jsonReturn) {
179
+ return walkType(jsonReturn, ctx);
180
+ }
181
+ }
167
182
  if (checker.isArrayType(type)) {
168
183
  const [element] = checker.getTypeArguments(type);
169
184
  return { type: "array", items: element ? walkType(element, ctx) : {} };
@@ -688,6 +703,42 @@ function urlSegment(segment) {
688
703
  }
689
704
  return { value: segment };
690
705
  }
706
+ function segmentRanks(segments) {
707
+ const ranks = [];
708
+ for (const segment of segments) {
709
+ const converted = urlSegment(segment);
710
+ if (!converted.value) {
711
+ continue;
712
+ }
713
+ if (converted.param?.catchAll) {
714
+ ranks.push({ rank: 2, text: converted.param.name });
715
+ } else if (converted.param) {
716
+ ranks.push({ rank: 1, text: converted.param.name });
717
+ } else {
718
+ ranks.push({ rank: 0, text: converted.value });
719
+ }
720
+ }
721
+ return ranks;
722
+ }
723
+ function compareRoutes(left, right) {
724
+ const leftRanks = segmentRanks(left.routeSegments);
725
+ const rightRanks = segmentRanks(right.routeSegments);
726
+ const shared = Math.min(leftRanks.length, rightRanks.length);
727
+ for (let i = 0; i < shared; i++) {
728
+ const a = leftRanks[i];
729
+ const b = rightRanks[i];
730
+ if (a.rank !== b.rank) {
731
+ return a.rank - b.rank;
732
+ }
733
+ if (a.rank === 0 && a.text !== b.text) {
734
+ return a.text.localeCompare(b.text);
735
+ }
736
+ }
737
+ if (leftRanks.length !== rightRanks.length) {
738
+ return leftRanks.length - rightRanks.length;
739
+ }
740
+ return METHOD_ORDER.indexOf(left.method) - METHOD_ORDER.indexOf(right.method);
741
+ }
691
742
  function pathFromSegments(segments) {
692
743
  const pathSegments = [];
693
744
  const params = [];
@@ -764,13 +815,7 @@ async function scanRoutes(routesDir) {
764
815
  sharedFiles: sharedFilesForDir(routesDir, routeDir, sharedCache)
765
816
  });
766
817
  }
767
- return routes.sort((left, right) => {
768
- const pathOrder = left.path.localeCompare(right.path);
769
- if (pathOrder !== 0) {
770
- return pathOrder;
771
- }
772
- return METHOD_ORDER.indexOf(left.method) - METHOD_ORDER.indexOf(right.method);
773
- });
818
+ return routes.sort(compareRoutes);
774
819
  }
775
820
 
776
821
  // src/types.ts