@unkey/api 0.13.1 → 0.15.0

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/index.d.mts CHANGED
@@ -258,6 +258,22 @@ interface paths {
258
258
  * @example 1000
259
259
  */
260
260
  remaining?: number;
261
+ /**
262
+ * @description Unkey enables you to refill verifications for each key at regular intervals.
263
+ * @example {
264
+ * "interval": "daily",
265
+ * "amount": 100
266
+ * }
267
+ */
268
+ refill?: {
269
+ /**
270
+ * @description Unkey will automatically refill verifications at the set interval.
271
+ * @enum {string}
272
+ */
273
+ interval: "daily" | "monthly";
274
+ /** @description The number of verifications to refill for each occurrence is determined individually for each key. */
275
+ amount: number;
276
+ };
261
277
  /**
262
278
  * @description Unkey comes with per-key ratelimiting out of the box.
263
279
  * @example {
@@ -445,12 +461,12 @@ interface paths {
445
461
  * Possible values are:
446
462
  * - NOT_FOUND: the key does not exist or has expired
447
463
  * - FORBIDDEN: the key is not allowed to access the api
448
- * - KEY_USAGE_EXCEEDED: the key has exceeded its request limit
449
- * - RATELIMITED: the key has been ratelimited,
464
+ * - USAGE_EXCEEDED: the key has exceeded its request limit
465
+ * - RATE_LIMITED: the key has been ratelimited,
450
466
  *
451
467
  * @enum {string}
452
468
  */
453
- code?: "NOT_FOUND" | "FORBIDDEN" | "KEY_USAGE_EXCEEDED" | "RATELIMITED";
469
+ code?: "NOT_FOUND" | "FORBIDDEN" | "USAGE_EXCEEDED" | "RATE_LIMITED";
454
470
  };
455
471
  };
456
472
  };
@@ -564,6 +580,22 @@ interface paths {
564
580
  * @example 1000
565
581
  */
566
582
  remaining?: number | null;
583
+ /**
584
+ * @description Unkey enables you to refill verifications for each key at regular intervals.
585
+ * @example {
586
+ * "interval": "daily",
587
+ * "amount": 100
588
+ * }
589
+ */
590
+ refill?: ({
591
+ /**
592
+ * @description Unkey will automatically refill verifications at the set interval. If null is used the refill functionality will be removed from the key.
593
+ * @enum {string}
594
+ */
595
+ interval: "daily" | "monthly";
596
+ /** @description The amount of verifications to refill for each occurrence is determined individually for each key. */
597
+ amount: number;
598
+ }) | null;
567
599
  };
568
600
  };
569
601
  };
@@ -700,6 +732,178 @@ interface paths {
700
732
  };
701
733
  };
702
734
  };
735
+ "/v1/keys.getVerifications": {
736
+ get: {
737
+ parameters: {
738
+ query?: {
739
+ keyId?: string;
740
+ ownerId?: string;
741
+ start?: number;
742
+ end?: number;
743
+ granularity?: "day";
744
+ };
745
+ };
746
+ responses: {
747
+ /** @description The configuration for a single key */
748
+ 200: {
749
+ content: {
750
+ "application/json": {
751
+ verifications: {
752
+ /**
753
+ * @description The timestamp of the usage data
754
+ * @example 1620000000000
755
+ */
756
+ time: number;
757
+ /**
758
+ * @description The number of successful requests
759
+ * @example 100
760
+ */
761
+ success: number;
762
+ /**
763
+ * @description The number of requests that were rate limited
764
+ * @example 10
765
+ */
766
+ rateLimited: number;
767
+ /**
768
+ * @description The number of requests that exceeded the usage limit
769
+ * @example 0
770
+ */
771
+ usageExceeded: number;
772
+ }[];
773
+ };
774
+ };
775
+ };
776
+ /** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
777
+ 400: {
778
+ content: {
779
+ "application/json": components["schemas"]["ErrBadRequest"];
780
+ };
781
+ };
782
+ /** @description Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. */
783
+ 401: {
784
+ content: {
785
+ "application/json": components["schemas"]["ErrUnauthorized"];
786
+ };
787
+ };
788
+ /** @description The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server. */
789
+ 403: {
790
+ content: {
791
+ "application/json": components["schemas"]["ErrForbidden"];
792
+ };
793
+ };
794
+ /** @description The server cannot find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web. */
795
+ 404: {
796
+ content: {
797
+ "application/json": components["schemas"]["ErrNotFound"];
798
+ };
799
+ };
800
+ /** @description This response is sent when a request conflicts with the current state of the server. */
801
+ 409: {
802
+ content: {
803
+ "application/json": components["schemas"]["ErrConflict"];
804
+ };
805
+ };
806
+ /** @description The user has sent too many requests in a given amount of time ("rate limiting") */
807
+ 429: {
808
+ content: {
809
+ "application/json": components["schemas"]["ErrTooManyRequests"];
810
+ };
811
+ };
812
+ /** @description The server has encountered a situation it does not know how to handle. */
813
+ 500: {
814
+ content: {
815
+ "application/json": components["schemas"]["ErrInternalServerError"];
816
+ };
817
+ };
818
+ };
819
+ };
820
+ };
821
+ "/vx/keys.getVerifications": {
822
+ get: {
823
+ parameters: {
824
+ query?: {
825
+ keyId?: string;
826
+ ownerId?: string;
827
+ start?: number | null;
828
+ end?: number | null;
829
+ granularity?: "day";
830
+ };
831
+ };
832
+ responses: {
833
+ /** @description The configuration for a single key */
834
+ 200: {
835
+ content: {
836
+ "application/json": {
837
+ verifications: {
838
+ /**
839
+ * @description The timestamp of the usage data
840
+ * @example 1620000000000
841
+ */
842
+ time: number;
843
+ /**
844
+ * @description The number of successful requests
845
+ * @example 100
846
+ */
847
+ success: number;
848
+ /**
849
+ * @description The number of requests that were rate limited
850
+ * @example 10
851
+ */
852
+ rateLimited: number;
853
+ /**
854
+ * @description The number of requests that exceeded the usage limit
855
+ * @example 0
856
+ */
857
+ usageExceeded: number;
858
+ }[];
859
+ };
860
+ };
861
+ };
862
+ /** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
863
+ 400: {
864
+ content: {
865
+ "application/json": components["schemas"]["ErrBadRequest"];
866
+ };
867
+ };
868
+ /** @description Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. */
869
+ 401: {
870
+ content: {
871
+ "application/json": components["schemas"]["ErrUnauthorized"];
872
+ };
873
+ };
874
+ /** @description The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server. */
875
+ 403: {
876
+ content: {
877
+ "application/json": components["schemas"]["ErrForbidden"];
878
+ };
879
+ };
880
+ /** @description The server cannot find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web. */
881
+ 404: {
882
+ content: {
883
+ "application/json": components["schemas"]["ErrNotFound"];
884
+ };
885
+ };
886
+ /** @description This response is sent when a request conflicts with the current state of the server. */
887
+ 409: {
888
+ content: {
889
+ "application/json": components["schemas"]["ErrConflict"];
890
+ };
891
+ };
892
+ /** @description The user has sent too many requests in a given amount of time ("rate limiting") */
893
+ 429: {
894
+ content: {
895
+ "application/json": components["schemas"]["ErrTooManyRequests"];
896
+ };
897
+ };
898
+ /** @description The server has encountered a situation it does not know how to handle. */
899
+ 500: {
900
+ content: {
901
+ "application/json": components["schemas"]["ErrInternalServerError"];
902
+ };
903
+ };
904
+ };
905
+ };
906
+ };
703
907
  "/v1/apis.getApi": {
704
908
  get: {
705
909
  parameters: {
@@ -1431,7 +1635,7 @@ interface paths {
1431
1635
  * @description The unix timestamp in milliseconds when the key was created
1432
1636
  * @example 0
1433
1637
  */
1434
- createdAt: number;
1638
+ createdAt?: number;
1435
1639
  /**
1436
1640
  * @description The unix timestamp in milliseconds when the key was deleted. We don't delete the key outright, you can restore it later.
1437
1641
  * @example 0
@@ -1477,13 +1681,13 @@ interface paths {
1477
1681
  * Possible values are:
1478
1682
  * - NOT_FOUND: the key does not exist or has expired
1479
1683
  * - FORBIDDEN: the key is not allowed to access the api
1480
- * - KEY_USAGE_EXCEEDED: the key has exceeded its request limit
1481
- * - RATELIMITED: the key has been ratelimited,
1684
+ * - USAGE_EXCEEDED: the key has exceeded its request limit
1685
+ * - RATE_LIMITED: the key has been ratelimited,
1482
1686
  *
1483
1687
  * @example NOT_FOUND
1484
1688
  * @enum {string}
1485
1689
  */
1486
- code?: "NOT_FOUND" | "FORBIDDEN" | "KEY_USAGE_EXCEEDED" | "RATELIMITED";
1690
+ code?: "NOT_FOUND" | "FORBIDDEN" | "USAGE_EXCEEDED" | "RATE_LIMITED";
1487
1691
  };
1488
1692
  };
1489
1693
  };
@@ -1820,7 +2024,7 @@ interface components {
1820
2024
  code: "BAD_REQUEST";
1821
2025
  /**
1822
2026
  * @description A link to our documentation with more details about this error code
1823
- * @example https://docs.unkey.dev/api-reference/errors/code/BAD_REQUEST
2027
+ * @example https://unkey.dev/docs/api-reference/errors/code/BAD_REQUEST
1824
2028
  */
1825
2029
  docs: string;
1826
2030
  /** @description A human readable explanation of what went wrong */
@@ -1842,7 +2046,7 @@ interface components {
1842
2046
  code: "UNAUTHORIZED";
1843
2047
  /**
1844
2048
  * @description A link to our documentation with more details about this error code
1845
- * @example https://docs.unkey.dev/api-reference/errors/code/UNAUTHORIZED
2049
+ * @example https://unkey.dev/docs/api-reference/errors/code/UNAUTHORIZED
1846
2050
  */
1847
2051
  docs: string;
1848
2052
  /** @description A human readable explanation of what went wrong */
@@ -1864,7 +2068,7 @@ interface components {
1864
2068
  code: "FORBIDDEN";
1865
2069
  /**
1866
2070
  * @description A link to our documentation with more details about this error code
1867
- * @example https://docs.unkey.dev/api-reference/errors/code/FORBIDDEN
2071
+ * @example https://unkey.dev/docs/api-reference/errors/code/FORBIDDEN
1868
2072
  */
1869
2073
  docs: string;
1870
2074
  /** @description A human readable explanation of what went wrong */
@@ -1886,7 +2090,7 @@ interface components {
1886
2090
  code: "NOT_FOUND";
1887
2091
  /**
1888
2092
  * @description A link to our documentation with more details about this error code
1889
- * @example https://docs.unkey.dev/api-reference/errors/code/NOT_FOUND
2093
+ * @example https://unkey.dev/docs/api-reference/errors/code/NOT_FOUND
1890
2094
  */
1891
2095
  docs: string;
1892
2096
  /** @description A human readable explanation of what went wrong */
@@ -1908,7 +2112,7 @@ interface components {
1908
2112
  code: "CONFLICT";
1909
2113
  /**
1910
2114
  * @description A link to our documentation with more details about this error code
1911
- * @example https://docs.unkey.dev/api-reference/errors/code/CONFLICT
2115
+ * @example https://unkey.dev/docs/api-reference/errors/code/CONFLICT
1912
2116
  */
1913
2117
  docs: string;
1914
2118
  /** @description A human readable explanation of what went wrong */
@@ -1930,7 +2134,7 @@ interface components {
1930
2134
  code: "TOO_MANY_REQUESTS";
1931
2135
  /**
1932
2136
  * @description A link to our documentation with more details about this error code
1933
- * @example https://docs.unkey.dev/api-reference/errors/code/TOO_MANY_REQUESTS
2137
+ * @example https://unkey.dev/docs/api-reference/errors/code/TOO_MANY_REQUESTS
1934
2138
  */
1935
2139
  docs: string;
1936
2140
  /** @description A human readable explanation of what went wrong */
@@ -1952,7 +2156,7 @@ interface components {
1952
2156
  code: "INTERNAL_SERVER_ERROR";
1953
2157
  /**
1954
2158
  * @description A link to our documentation with more details about this error code
1955
- * @example https://docs.unkey.dev/api-reference/errors/code/INTERNAL_SERVER_ERROR
2159
+ * @example https://unkey.dev/docs/api-reference/errors/code/INTERNAL_SERVER_ERROR
1956
2160
  */
1957
2161
  docs: string;
1958
2162
  /** @description A human readable explanation of what went wrong */
@@ -1986,7 +2190,7 @@ interface components {
1986
2190
  */
1987
2191
  apiId?: string;
1988
2192
  /**
1989
- * @description The name of the key, give keys a name to easily identifiy their purpose
2193
+ * @description The name of the key, give keys a name to easily identify their purpose
1990
2194
  * @example Customer X
1991
2195
  */
1992
2196
  name?: string;
@@ -2012,7 +2216,7 @@ interface components {
2012
2216
  * @description The unix timestamp in milliseconds when the key was created
2013
2217
  * @example 0
2014
2218
  */
2015
- createdAt: number;
2219
+ createdAt?: number;
2016
2220
  /**
2017
2221
  * @description The unix timestamp in milliseconds when the key was deleted. We don't delete the key outright, you can restore it later.
2018
2222
  * @example 0
@@ -2028,6 +2232,31 @@ interface components {
2028
2232
  * @example 1000
2029
2233
  */
2030
2234
  remaining?: number;
2235
+ /**
2236
+ * @description Unkey allows you to refill remaining verifications on a key on a regular interval.
2237
+ * @example {
2238
+ * "interval": "daily",
2239
+ * "amount": 10
2240
+ * }
2241
+ */
2242
+ refill?: {
2243
+ /**
2244
+ * @description Determines the rate at which verifications will be refilled.
2245
+ * @example daily
2246
+ * @enum {string}
2247
+ */
2248
+ interval: "daily" | "monthly";
2249
+ /**
2250
+ * @description Resets `remaining` to this value every interval.
2251
+ * @example 100
2252
+ */
2253
+ amount: number;
2254
+ /**
2255
+ * @description The unix timestamp in miliseconds when the key was last refilled.
2256
+ * @example 100
2257
+ */
2258
+ lastRefillAt?: number;
2259
+ };
2031
2260
  /**
2032
2261
  * @description Unkey comes with per-key ratelimiting out of the box.
2033
2262
  * @example {
@@ -2142,6 +2371,7 @@ declare class Unkey {
2142
2371
  delete: (req: paths["/v1/keys.deleteKey"]["post"]["requestBody"]["content"]["application/json"]) => Promise<Result<paths["/v1/keys.deleteKey"]["post"]["responses"]["200"]["content"]["application/json"]>>;
2143
2372
  updateRemaining: (req: paths["/v1/keys.updateRemaining"]["post"]["requestBody"]["content"]["application/json"]) => Promise<Result<paths["/v1/keys.updateRemaining"]["post"]["responses"]["200"]["content"]["application/json"]>>;
2144
2373
  get: (req: paths["/v1/keys.getKey"]["get"]["parameters"]["query"]) => Promise<Result<paths["/v1/keys.getKey"]["get"]["responses"]["200"]["content"]["application/json"]>>;
2374
+ getVerifications: (req: paths["/v1/keys.getVerifications"]["get"]["parameters"]["query"]) => Promise<Result<paths["/v1/keys.getVerifications"]["get"]["responses"]["200"]["content"]["application/json"]>>;
2145
2375
  };
2146
2376
  get apis(): {
2147
2377
  create: (req: paths["/v1/apis.createApi"]["post"]["requestBody"]["content"]["application/json"]) => Promise<Result<paths["/v1/apis.createApi"]["post"]["responses"]["200"]["content"]["application/json"]>>;
@@ -2199,7 +2429,7 @@ declare function verifyKey(req: string | {
2199
2429
  reset: number;
2200
2430
  } | undefined;
2201
2431
  remaining?: number | undefined;
2202
- code?: "NOT_FOUND" | "FORBIDDEN" | "KEY_USAGE_EXCEEDED" | "RATELIMITED" | undefined;
2432
+ code?: "NOT_FOUND" | "FORBIDDEN" | "USAGE_EXCEEDED" | "RATE_LIMITED" | undefined;
2203
2433
  };
2204
2434
  error?: undefined;
2205
2435
  }>;
package/dist/index.d.ts CHANGED
@@ -258,6 +258,22 @@ interface paths {
258
258
  * @example 1000
259
259
  */
260
260
  remaining?: number;
261
+ /**
262
+ * @description Unkey enables you to refill verifications for each key at regular intervals.
263
+ * @example {
264
+ * "interval": "daily",
265
+ * "amount": 100
266
+ * }
267
+ */
268
+ refill?: {
269
+ /**
270
+ * @description Unkey will automatically refill verifications at the set interval.
271
+ * @enum {string}
272
+ */
273
+ interval: "daily" | "monthly";
274
+ /** @description The number of verifications to refill for each occurrence is determined individually for each key. */
275
+ amount: number;
276
+ };
261
277
  /**
262
278
  * @description Unkey comes with per-key ratelimiting out of the box.
263
279
  * @example {
@@ -445,12 +461,12 @@ interface paths {
445
461
  * Possible values are:
446
462
  * - NOT_FOUND: the key does not exist or has expired
447
463
  * - FORBIDDEN: the key is not allowed to access the api
448
- * - KEY_USAGE_EXCEEDED: the key has exceeded its request limit
449
- * - RATELIMITED: the key has been ratelimited,
464
+ * - USAGE_EXCEEDED: the key has exceeded its request limit
465
+ * - RATE_LIMITED: the key has been ratelimited,
450
466
  *
451
467
  * @enum {string}
452
468
  */
453
- code?: "NOT_FOUND" | "FORBIDDEN" | "KEY_USAGE_EXCEEDED" | "RATELIMITED";
469
+ code?: "NOT_FOUND" | "FORBIDDEN" | "USAGE_EXCEEDED" | "RATE_LIMITED";
454
470
  };
455
471
  };
456
472
  };
@@ -564,6 +580,22 @@ interface paths {
564
580
  * @example 1000
565
581
  */
566
582
  remaining?: number | null;
583
+ /**
584
+ * @description Unkey enables you to refill verifications for each key at regular intervals.
585
+ * @example {
586
+ * "interval": "daily",
587
+ * "amount": 100
588
+ * }
589
+ */
590
+ refill?: ({
591
+ /**
592
+ * @description Unkey will automatically refill verifications at the set interval. If null is used the refill functionality will be removed from the key.
593
+ * @enum {string}
594
+ */
595
+ interval: "daily" | "monthly";
596
+ /** @description The amount of verifications to refill for each occurrence is determined individually for each key. */
597
+ amount: number;
598
+ }) | null;
567
599
  };
568
600
  };
569
601
  };
@@ -700,6 +732,178 @@ interface paths {
700
732
  };
701
733
  };
702
734
  };
735
+ "/v1/keys.getVerifications": {
736
+ get: {
737
+ parameters: {
738
+ query?: {
739
+ keyId?: string;
740
+ ownerId?: string;
741
+ start?: number;
742
+ end?: number;
743
+ granularity?: "day";
744
+ };
745
+ };
746
+ responses: {
747
+ /** @description The configuration for a single key */
748
+ 200: {
749
+ content: {
750
+ "application/json": {
751
+ verifications: {
752
+ /**
753
+ * @description The timestamp of the usage data
754
+ * @example 1620000000000
755
+ */
756
+ time: number;
757
+ /**
758
+ * @description The number of successful requests
759
+ * @example 100
760
+ */
761
+ success: number;
762
+ /**
763
+ * @description The number of requests that were rate limited
764
+ * @example 10
765
+ */
766
+ rateLimited: number;
767
+ /**
768
+ * @description The number of requests that exceeded the usage limit
769
+ * @example 0
770
+ */
771
+ usageExceeded: number;
772
+ }[];
773
+ };
774
+ };
775
+ };
776
+ /** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
777
+ 400: {
778
+ content: {
779
+ "application/json": components["schemas"]["ErrBadRequest"];
780
+ };
781
+ };
782
+ /** @description Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. */
783
+ 401: {
784
+ content: {
785
+ "application/json": components["schemas"]["ErrUnauthorized"];
786
+ };
787
+ };
788
+ /** @description The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server. */
789
+ 403: {
790
+ content: {
791
+ "application/json": components["schemas"]["ErrForbidden"];
792
+ };
793
+ };
794
+ /** @description The server cannot find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web. */
795
+ 404: {
796
+ content: {
797
+ "application/json": components["schemas"]["ErrNotFound"];
798
+ };
799
+ };
800
+ /** @description This response is sent when a request conflicts with the current state of the server. */
801
+ 409: {
802
+ content: {
803
+ "application/json": components["schemas"]["ErrConflict"];
804
+ };
805
+ };
806
+ /** @description The user has sent too many requests in a given amount of time ("rate limiting") */
807
+ 429: {
808
+ content: {
809
+ "application/json": components["schemas"]["ErrTooManyRequests"];
810
+ };
811
+ };
812
+ /** @description The server has encountered a situation it does not know how to handle. */
813
+ 500: {
814
+ content: {
815
+ "application/json": components["schemas"]["ErrInternalServerError"];
816
+ };
817
+ };
818
+ };
819
+ };
820
+ };
821
+ "/vx/keys.getVerifications": {
822
+ get: {
823
+ parameters: {
824
+ query?: {
825
+ keyId?: string;
826
+ ownerId?: string;
827
+ start?: number | null;
828
+ end?: number | null;
829
+ granularity?: "day";
830
+ };
831
+ };
832
+ responses: {
833
+ /** @description The configuration for a single key */
834
+ 200: {
835
+ content: {
836
+ "application/json": {
837
+ verifications: {
838
+ /**
839
+ * @description The timestamp of the usage data
840
+ * @example 1620000000000
841
+ */
842
+ time: number;
843
+ /**
844
+ * @description The number of successful requests
845
+ * @example 100
846
+ */
847
+ success: number;
848
+ /**
849
+ * @description The number of requests that were rate limited
850
+ * @example 10
851
+ */
852
+ rateLimited: number;
853
+ /**
854
+ * @description The number of requests that exceeded the usage limit
855
+ * @example 0
856
+ */
857
+ usageExceeded: number;
858
+ }[];
859
+ };
860
+ };
861
+ };
862
+ /** @description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing). */
863
+ 400: {
864
+ content: {
865
+ "application/json": components["schemas"]["ErrBadRequest"];
866
+ };
867
+ };
868
+ /** @description Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated". That is, the client must authenticate itself to get the requested response. */
869
+ 401: {
870
+ content: {
871
+ "application/json": components["schemas"]["ErrUnauthorized"];
872
+ };
873
+ };
874
+ /** @description The client does not have access rights to the content; that is, it is unauthorized, so the server is refusing to give the requested resource. Unlike 401 Unauthorized, the client's identity is known to the server. */
875
+ 403: {
876
+ content: {
877
+ "application/json": components["schemas"]["ErrForbidden"];
878
+ };
879
+ };
880
+ /** @description The server cannot find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web. */
881
+ 404: {
882
+ content: {
883
+ "application/json": components["schemas"]["ErrNotFound"];
884
+ };
885
+ };
886
+ /** @description This response is sent when a request conflicts with the current state of the server. */
887
+ 409: {
888
+ content: {
889
+ "application/json": components["schemas"]["ErrConflict"];
890
+ };
891
+ };
892
+ /** @description The user has sent too many requests in a given amount of time ("rate limiting") */
893
+ 429: {
894
+ content: {
895
+ "application/json": components["schemas"]["ErrTooManyRequests"];
896
+ };
897
+ };
898
+ /** @description The server has encountered a situation it does not know how to handle. */
899
+ 500: {
900
+ content: {
901
+ "application/json": components["schemas"]["ErrInternalServerError"];
902
+ };
903
+ };
904
+ };
905
+ };
906
+ };
703
907
  "/v1/apis.getApi": {
704
908
  get: {
705
909
  parameters: {
@@ -1431,7 +1635,7 @@ interface paths {
1431
1635
  * @description The unix timestamp in milliseconds when the key was created
1432
1636
  * @example 0
1433
1637
  */
1434
- createdAt: number;
1638
+ createdAt?: number;
1435
1639
  /**
1436
1640
  * @description The unix timestamp in milliseconds when the key was deleted. We don't delete the key outright, you can restore it later.
1437
1641
  * @example 0
@@ -1477,13 +1681,13 @@ interface paths {
1477
1681
  * Possible values are:
1478
1682
  * - NOT_FOUND: the key does not exist or has expired
1479
1683
  * - FORBIDDEN: the key is not allowed to access the api
1480
- * - KEY_USAGE_EXCEEDED: the key has exceeded its request limit
1481
- * - RATELIMITED: the key has been ratelimited,
1684
+ * - USAGE_EXCEEDED: the key has exceeded its request limit
1685
+ * - RATE_LIMITED: the key has been ratelimited,
1482
1686
  *
1483
1687
  * @example NOT_FOUND
1484
1688
  * @enum {string}
1485
1689
  */
1486
- code?: "NOT_FOUND" | "FORBIDDEN" | "KEY_USAGE_EXCEEDED" | "RATELIMITED";
1690
+ code?: "NOT_FOUND" | "FORBIDDEN" | "USAGE_EXCEEDED" | "RATE_LIMITED";
1487
1691
  };
1488
1692
  };
1489
1693
  };
@@ -1820,7 +2024,7 @@ interface components {
1820
2024
  code: "BAD_REQUEST";
1821
2025
  /**
1822
2026
  * @description A link to our documentation with more details about this error code
1823
- * @example https://docs.unkey.dev/api-reference/errors/code/BAD_REQUEST
2027
+ * @example https://unkey.dev/docs/api-reference/errors/code/BAD_REQUEST
1824
2028
  */
1825
2029
  docs: string;
1826
2030
  /** @description A human readable explanation of what went wrong */
@@ -1842,7 +2046,7 @@ interface components {
1842
2046
  code: "UNAUTHORIZED";
1843
2047
  /**
1844
2048
  * @description A link to our documentation with more details about this error code
1845
- * @example https://docs.unkey.dev/api-reference/errors/code/UNAUTHORIZED
2049
+ * @example https://unkey.dev/docs/api-reference/errors/code/UNAUTHORIZED
1846
2050
  */
1847
2051
  docs: string;
1848
2052
  /** @description A human readable explanation of what went wrong */
@@ -1864,7 +2068,7 @@ interface components {
1864
2068
  code: "FORBIDDEN";
1865
2069
  /**
1866
2070
  * @description A link to our documentation with more details about this error code
1867
- * @example https://docs.unkey.dev/api-reference/errors/code/FORBIDDEN
2071
+ * @example https://unkey.dev/docs/api-reference/errors/code/FORBIDDEN
1868
2072
  */
1869
2073
  docs: string;
1870
2074
  /** @description A human readable explanation of what went wrong */
@@ -1886,7 +2090,7 @@ interface components {
1886
2090
  code: "NOT_FOUND";
1887
2091
  /**
1888
2092
  * @description A link to our documentation with more details about this error code
1889
- * @example https://docs.unkey.dev/api-reference/errors/code/NOT_FOUND
2093
+ * @example https://unkey.dev/docs/api-reference/errors/code/NOT_FOUND
1890
2094
  */
1891
2095
  docs: string;
1892
2096
  /** @description A human readable explanation of what went wrong */
@@ -1908,7 +2112,7 @@ interface components {
1908
2112
  code: "CONFLICT";
1909
2113
  /**
1910
2114
  * @description A link to our documentation with more details about this error code
1911
- * @example https://docs.unkey.dev/api-reference/errors/code/CONFLICT
2115
+ * @example https://unkey.dev/docs/api-reference/errors/code/CONFLICT
1912
2116
  */
1913
2117
  docs: string;
1914
2118
  /** @description A human readable explanation of what went wrong */
@@ -1930,7 +2134,7 @@ interface components {
1930
2134
  code: "TOO_MANY_REQUESTS";
1931
2135
  /**
1932
2136
  * @description A link to our documentation with more details about this error code
1933
- * @example https://docs.unkey.dev/api-reference/errors/code/TOO_MANY_REQUESTS
2137
+ * @example https://unkey.dev/docs/api-reference/errors/code/TOO_MANY_REQUESTS
1934
2138
  */
1935
2139
  docs: string;
1936
2140
  /** @description A human readable explanation of what went wrong */
@@ -1952,7 +2156,7 @@ interface components {
1952
2156
  code: "INTERNAL_SERVER_ERROR";
1953
2157
  /**
1954
2158
  * @description A link to our documentation with more details about this error code
1955
- * @example https://docs.unkey.dev/api-reference/errors/code/INTERNAL_SERVER_ERROR
2159
+ * @example https://unkey.dev/docs/api-reference/errors/code/INTERNAL_SERVER_ERROR
1956
2160
  */
1957
2161
  docs: string;
1958
2162
  /** @description A human readable explanation of what went wrong */
@@ -1986,7 +2190,7 @@ interface components {
1986
2190
  */
1987
2191
  apiId?: string;
1988
2192
  /**
1989
- * @description The name of the key, give keys a name to easily identifiy their purpose
2193
+ * @description The name of the key, give keys a name to easily identify their purpose
1990
2194
  * @example Customer X
1991
2195
  */
1992
2196
  name?: string;
@@ -2012,7 +2216,7 @@ interface components {
2012
2216
  * @description The unix timestamp in milliseconds when the key was created
2013
2217
  * @example 0
2014
2218
  */
2015
- createdAt: number;
2219
+ createdAt?: number;
2016
2220
  /**
2017
2221
  * @description The unix timestamp in milliseconds when the key was deleted. We don't delete the key outright, you can restore it later.
2018
2222
  * @example 0
@@ -2028,6 +2232,31 @@ interface components {
2028
2232
  * @example 1000
2029
2233
  */
2030
2234
  remaining?: number;
2235
+ /**
2236
+ * @description Unkey allows you to refill remaining verifications on a key on a regular interval.
2237
+ * @example {
2238
+ * "interval": "daily",
2239
+ * "amount": 10
2240
+ * }
2241
+ */
2242
+ refill?: {
2243
+ /**
2244
+ * @description Determines the rate at which verifications will be refilled.
2245
+ * @example daily
2246
+ * @enum {string}
2247
+ */
2248
+ interval: "daily" | "monthly";
2249
+ /**
2250
+ * @description Resets `remaining` to this value every interval.
2251
+ * @example 100
2252
+ */
2253
+ amount: number;
2254
+ /**
2255
+ * @description The unix timestamp in miliseconds when the key was last refilled.
2256
+ * @example 100
2257
+ */
2258
+ lastRefillAt?: number;
2259
+ };
2031
2260
  /**
2032
2261
  * @description Unkey comes with per-key ratelimiting out of the box.
2033
2262
  * @example {
@@ -2142,6 +2371,7 @@ declare class Unkey {
2142
2371
  delete: (req: paths["/v1/keys.deleteKey"]["post"]["requestBody"]["content"]["application/json"]) => Promise<Result<paths["/v1/keys.deleteKey"]["post"]["responses"]["200"]["content"]["application/json"]>>;
2143
2372
  updateRemaining: (req: paths["/v1/keys.updateRemaining"]["post"]["requestBody"]["content"]["application/json"]) => Promise<Result<paths["/v1/keys.updateRemaining"]["post"]["responses"]["200"]["content"]["application/json"]>>;
2144
2373
  get: (req: paths["/v1/keys.getKey"]["get"]["parameters"]["query"]) => Promise<Result<paths["/v1/keys.getKey"]["get"]["responses"]["200"]["content"]["application/json"]>>;
2374
+ getVerifications: (req: paths["/v1/keys.getVerifications"]["get"]["parameters"]["query"]) => Promise<Result<paths["/v1/keys.getVerifications"]["get"]["responses"]["200"]["content"]["application/json"]>>;
2145
2375
  };
2146
2376
  get apis(): {
2147
2377
  create: (req: paths["/v1/apis.createApi"]["post"]["requestBody"]["content"]["application/json"]) => Promise<Result<paths["/v1/apis.createApi"]["post"]["responses"]["200"]["content"]["application/json"]>>;
@@ -2199,7 +2429,7 @@ declare function verifyKey(req: string | {
2199
2429
  reset: number;
2200
2430
  } | undefined;
2201
2431
  remaining?: number | undefined;
2202
- code?: "NOT_FOUND" | "FORBIDDEN" | "KEY_USAGE_EXCEEDED" | "RATELIMITED" | undefined;
2432
+ code?: "NOT_FOUND" | "FORBIDDEN" | "USAGE_EXCEEDED" | "RATE_LIMITED" | undefined;
2203
2433
  };
2204
2434
  error?: undefined;
2205
2435
  }>;
package/dist/index.js CHANGED
@@ -26,7 +26,7 @@ __export(src_exports, {
26
26
  module.exports = __toCommonJS(src_exports);
27
27
 
28
28
  // package.json
29
- var version = "0.13.1";
29
+ var version = "0.15.0";
30
30
 
31
31
  // src/client.ts
32
32
  var Unkey = class {
@@ -148,6 +148,13 @@ var Unkey = class {
148
148
  method: "GET",
149
149
  query: req
150
150
  });
151
+ },
152
+ getVerifications: async (req) => {
153
+ return await this.fetch({
154
+ path: ["v1", "keys.getVerifications"],
155
+ method: "GET",
156
+ query: req
157
+ });
151
158
  }
152
159
  };
153
160
  }
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../package.json","../src/client.ts","../src/verify.ts"],"sourcesContent":["export * from \"./client\";\nexport * from \"./verify\";\nexport * from \"./errors\";\n","{\n \"name\": \"@unkey/api\",\n \"version\": \"0.13.1\",\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"license\": \"MIT\",\n \"private\": false,\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"keywords\": [\n \"unkey\",\n \"client\",\n \"api\"\n ],\n \"bugs\": {\n \"url\": \"https://github.com/unkeyed/unkey/issues\"\n },\n \"homepage\": \"https://github.com/unkeyed/unkey#readme\",\n \"files\": [\n \"./dist/**\"\n ],\n \"author\": \"Andreas Thomas <andreas@chronark.com>\",\n \"scripts\": {\n \"test\": \"bun test\",\n \"test:coverage\": \"bun test --coverage\",\n \"generate\": \"openapi-typescript https://api.unkey.dev/openapi.json -o ./src/openapi.d.ts\",\n \"build\": \"pnpm generate && tsup\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^20.8.7\",\n \"openapi-typescript\": \"^6.7.2\",\n \"tsup\": \"^7.2.0\",\n \"tsx\": \"^3.14.0\",\n \"typescript\": \"^5.2.2\"\n }\n}\n","import { version } from \"../package.json\";\nimport { ErrorResponse } from \"./errors\";\nimport type { paths } from \"./openapi\";\nexport type UnkeyOptions = (\n | {\n token?: never;\n\n /**\n * The root key from unkey.dev.\n *\n * You can create/manage your root keys here:\n * https://unkey.dev/app/settings/root-keys\n */\n rootKey: string;\n }\n | {\n /**\n * The workspace key from unkey.dev\n *\n * @deprecated Use `rootKey`\n */\n token: string;\n rootKey?: never;\n }\n) & {\n /**\n * @default https://api.unkey.dev\n */\n baseUrl?: string;\n\n /**\n * Retry on network errors\n */\n retry?: {\n /**\n * How many attempts should be made\n * The maximum number of requests will be `attempts + 1`\n * `0` means no retries\n *\n * @default 5\n */\n attempts?: number;\n /**\n * Return how many milliseconds to wait until the next attempt is made\n *\n * @default `(retryCount) => Math.round(Math.exp(retryCount) * 10)),`\n */\n backoff?: (retryCount: number) => number;\n };\n /**\n * Customize the `fetch` cache behaviour\n */\n cache?: RequestCache;\n\n /**\n * The version of the SDK instantiating this client.\n *\n * This is used for internal metrics and is not covered by semver, and may change at any time.\n *\n * You can leave this blank unless you are building a wrapper around this SDK.\n */\n wrapperSdkVersion?: `v${string}`;\n};\n\ntype ApiRequest = {\n path: string[];\n} & (\n | {\n method: \"GET\";\n body?: never;\n query?: Record<string, string | number | boolean>;\n }\n | {\n method: \"POST\";\n body?: unknown;\n query?: never;\n }\n);\n\ntype Result<R> =\n | {\n result: R;\n error?: never;\n }\n | {\n result?: never;\n error: ErrorResponse[\"error\"];\n };\n\nexport class Unkey {\n public readonly baseUrl: string;\n private readonly rootKey: string;\n private readonly cache?: RequestCache;\n private readonly sdkVersions: `v${string}`[] = [];\n\n public readonly retry: {\n attempts: number;\n backoff: (retryCount: number) => number;\n };\n\n constructor(opts: UnkeyOptions) {\n this.baseUrl = opts.baseUrl ?? \"https://api.unkey.dev\";\n this.rootKey = opts.rootKey ?? opts.token;\n this.sdkVersions.push(`v${version}`);\n if (opts.wrapperSdkVersion) {\n this.sdkVersions.push(opts.wrapperSdkVersion);\n }\n\n this.cache = opts.cache;\n /**\n * Even though typescript should prevent this, some people still pass undefined or empty strings\n */\n if (!this.rootKey) {\n throw new Error(\n \"Unkey root key must be set, maybe you passed in `undefined` or an empty string?\",\n );\n }\n\n this.retry = {\n attempts: opts.retry?.attempts ?? 5,\n backoff: opts.retry?.backoff ?? ((n) => Math.round(Math.exp(n) * 10)),\n };\n }\n\n private async fetch<TResult>(req: ApiRequest): Promise<Result<TResult>> {\n let res: Response | null = null;\n let err: Error | null = null;\n for (let i = 0; i <= this.retry.attempts; i++) {\n const url = new URL(`${this.baseUrl}/${req.path.join(\"/\")}`);\n if (req.query) {\n for (const [k, v] of Object.entries(req.query)) {\n url.searchParams.set(k, v.toString());\n }\n }\n res = await fetch(url, {\n method: req.method,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.rootKey}`,\n \"Unkey-SDK\": this.sdkVersions.join(\",\"),\n },\n cache: this.cache,\n body: JSON.stringify(req.body),\n }).catch((e: Error) => {\n err = e;\n return null; // set `res` to `null`\n });\n if (res?.ok) {\n return { result: (await res.json()) as TResult };\n }\n const backoff = this.retry.backoff(i);\n console.debug(\n \"attempt %d of %d to reach %s failed, retrying in %d ms: %s\",\n i + 1,\n this.retry.attempts + 1,\n url,\n backoff,\n // @ts-ignore I don't understand why `err` is `never`\n err?.message,\n );\n await new Promise((r) => setTimeout(r, backoff));\n }\n\n if (res) {\n return { error: (await res.json()) as ErrorResponse[\"error\"] };\n }\n\n return {\n error: {\n // @ts-ignore\n code: \"FETCH_ERROR\",\n // @ts-ignore I don't understand why `err` is `never`\n message: err?.message ?? \"No response\",\n docs: \"https://developer.mozilla.org/en-US/docs/Web/API/fetch\",\n requestId: \"N/A\",\n },\n };\n }\n\n public get keys() {\n return {\n create: async (\n req: paths[\"/v1/keys.createKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.createKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.createKey\"],\n method: \"POST\",\n body: req,\n });\n },\n update: async (\n req: paths[\"/v1/keys.updateKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.updateKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.updateKey\"],\n method: \"POST\",\n body: req,\n });\n },\n verify: async (\n req: paths[\"/v1/keys.verifyKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.verifyKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.verifyKey\"],\n method: \"POST\",\n body: req,\n });\n },\n delete: async (\n req: paths[\"/v1/keys.deleteKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.deleteKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.deleteKey\"],\n method: \"POST\",\n body: req,\n });\n },\n updateRemaining: async (\n req: paths[\"/v1/keys.updateRemaining\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.updateRemaining\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.updateRemaining\"],\n method: \"POST\",\n body: req,\n });\n },\n get: async (\n req: paths[\"/v1/keys.getKey\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/keys.getKey\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.getKey\"],\n method: \"GET\",\n query: req,\n });\n },\n };\n }\n\n public get apis() {\n return {\n create: async (\n req: paths[\"/v1/apis.createApi\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/apis.createApi\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.createApi\"],\n method: \"POST\",\n body: req,\n });\n },\n delete: async (\n req: paths[\"/v1/apis.deleteApi\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/apis.deleteApi\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.deleteApi\"],\n method: \"POST\",\n body: req,\n });\n },\n get: async (\n req: paths[\"/v1/apis.getApi\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/apis.getApi\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.getApi\"],\n method: \"GET\",\n query: req,\n });\n },\n listKeys: async (\n req: paths[\"/v1/apis.listKeys\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/apis.listKeys\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.listKeys\"],\n method: \"GET\",\n query: req,\n });\n },\n };\n }\n}\n","import { Unkey } from \"./client\";\n\n/**\n * Verify a key\n *\n * @example\n * ```ts\n * const { result, error } = await verifyKey(\"key_123\")\n * if (error){\n * // handle potential network or bad request error\n * // a link to our docs will be in the `error.docs` field\n * console.error(error.message)\n * return\n * }\n * if (!result.valid) {\n * // do not grant access\n * return\n * }\n *\n * // process request\n * console.log(result)\n * ```\n */\nexport function verifyKey(req: string | { key: string; apiId: string }) {\n // yes this is empty to make typescript happy but we don't need a token for verifying keys\n // it's not the cleanest but it works for now :)\n const unkey = new Unkey({ rootKey: \"public\" });\n return unkey.keys.verify(typeof req === \"string\" ? { key: req } : req);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEE,cAAW;;;ACuFN,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACC;AAAA,EACA;AAAA,EACA,cAA8B,CAAC;AAAA,EAEhC;AAAA,EAKhB,YAAY,MAAoB;AAC9B,SAAK,UAAU,KAAK,WAAW;AAC/B,SAAK,UAAU,KAAK,WAAW,KAAK;AACpC,SAAK,YAAY,KAAK,IAAI,OAAO,EAAE;AACnC,QAAI,KAAK,mBAAmB;AAC1B,WAAK,YAAY,KAAK,KAAK,iBAAiB;AAAA,IAC9C;AAEA,SAAK,QAAQ,KAAK;AAIlB,QAAI,CAAC,KAAK,SAAS;AACjB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,SAAK,QAAQ;AAAA,MACX,UAAU,KAAK,OAAO,YAAY;AAAA,MAClC,SAAS,KAAK,OAAO,YAAY,CAAC,MAAM,KAAK,MAAM,KAAK,IAAI,CAAC,IAAI,EAAE;AAAA,IACrE;AAAA,EACF;AAAA,EAEA,MAAc,MAAe,KAA2C;AACtE,QAAI,MAAuB;AAC3B,QAAI,MAAoB;AACxB,aAAS,IAAI,GAAG,KAAK,KAAK,MAAM,UAAU,KAAK;AAC7C,YAAM,MAAM,IAAI,IAAI,GAAG,KAAK,OAAO,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC,EAAE;AAC3D,UAAI,IAAI,OAAO;AACb,mBAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,IAAI,KAAK,GAAG;AAC9C,cAAI,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A,QACT,CAAC;AAAA,MACH;AAAA,MACA,UAAU,OACR,QAGG;AACH,eAAO,MAAM,KAAK,MAAM;AAAA,UACtB,MAAM,CAAC,MAAM,eAAe;AAAA,UAC5B,QAAQ;AAAA,UACR,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;;;ACjSO,SAAS,UAAU,KAA8C;AAGtE,QAAM,QAAQ,IAAI,MAAM,EAAE,SAAS,SAAS,CAAC;AAC7C,SAAO,MAAM,KAAK,OAAO,OAAO,QAAQ,WAAW,EAAE,KAAK,IAAI,IAAI,GAAG;AACvE;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../package.json","../src/client.ts","../src/verify.ts"],"sourcesContent":["export * from \"./client\";\nexport * from \"./verify\";\nexport * from \"./errors\";\n","{\n \"name\": \"@unkey/api\",\n \"version\": \"0.15.0\",\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"license\": \"MIT\",\n \"private\": false,\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"keywords\": [\n \"unkey\",\n \"client\",\n \"api\"\n ],\n \"bugs\": {\n \"url\": \"https://github.com/unkeyed/unkey/issues\"\n },\n \"homepage\": \"https://github.com/unkeyed/unkey#readme\",\n \"files\": [\n \"./dist/**\"\n ],\n \"author\": \"Andreas Thomas <andreas@chronark.com>\",\n \"scripts\": {\n \"test\": \"bun test\",\n \"test:coverage\": \"bun test --coverage\",\n \"generate\": \"openapi-typescript https://api.unkey.dev/openapi.json -o ./src/openapi.d.ts\",\n \"build\": \"pnpm generate && tsup\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^20.8.7\",\n \"@unkey/tsconfig\": \"workspace:^\",\n \"openapi-typescript\": \"^6.7.2\",\n \"tsup\": \"^7.2.0\",\n \"tsx\": \"^3.14.0\",\n \"typescript\": \"^5.2.2\"\n }\n}\n","import { version } from \"../package.json\";\nimport { ErrorResponse } from \"./errors\";\nimport type { paths } from \"./openapi\";\nexport type UnkeyOptions = (\n | {\n token?: never;\n\n /**\n * The root key from unkey.dev.\n *\n * You can create/manage your root keys here:\n * https://unkey.dev/app/settings/root-keys\n */\n rootKey: string;\n }\n | {\n /**\n * The workspace key from unkey.dev\n *\n * @deprecated Use `rootKey`\n */\n token: string;\n rootKey?: never;\n }\n) & {\n /**\n * @default https://api.unkey.dev\n */\n baseUrl?: string;\n\n /**\n * Retry on network errors\n */\n retry?: {\n /**\n * How many attempts should be made\n * The maximum number of requests will be `attempts + 1`\n * `0` means no retries\n *\n * @default 5\n */\n attempts?: number;\n /**\n * Return how many milliseconds to wait until the next attempt is made\n *\n * @default `(retryCount) => Math.round(Math.exp(retryCount) * 10)),`\n */\n backoff?: (retryCount: number) => number;\n };\n /**\n * Customize the `fetch` cache behaviour\n */\n cache?: RequestCache;\n\n /**\n * The version of the SDK instantiating this client.\n *\n * This is used for internal metrics and is not covered by semver, and may change at any time.\n *\n * You can leave this blank unless you are building a wrapper around this SDK.\n */\n wrapperSdkVersion?: `v${string}`;\n};\n\ntype ApiRequest = {\n path: string[];\n} & (\n | {\n method: \"GET\";\n body?: never;\n query?: Record<string, string | number | boolean>;\n }\n | {\n method: \"POST\";\n body?: unknown;\n query?: never;\n }\n);\n\ntype Result<R> =\n | {\n result: R;\n error?: never;\n }\n | {\n result?: never;\n error: ErrorResponse[\"error\"];\n };\n\nexport class Unkey {\n public readonly baseUrl: string;\n private readonly rootKey: string;\n private readonly cache?: RequestCache;\n private readonly sdkVersions: `v${string}`[] = [];\n\n public readonly retry: {\n attempts: number;\n backoff: (retryCount: number) => number;\n };\n\n constructor(opts: UnkeyOptions) {\n this.baseUrl = opts.baseUrl ?? \"https://api.unkey.dev\";\n this.rootKey = opts.rootKey ?? opts.token;\n this.sdkVersions.push(`v${version}`);\n if (opts.wrapperSdkVersion) {\n this.sdkVersions.push(opts.wrapperSdkVersion);\n }\n\n this.cache = opts.cache;\n /**\n * Even though typescript should prevent this, some people still pass undefined or empty strings\n */\n if (!this.rootKey) {\n throw new Error(\n \"Unkey root key must be set, maybe you passed in `undefined` or an empty string?\",\n );\n }\n\n this.retry = {\n attempts: opts.retry?.attempts ?? 5,\n backoff: opts.retry?.backoff ?? ((n) => Math.round(Math.exp(n) * 10)),\n };\n }\n\n private async fetch<TResult>(req: ApiRequest): Promise<Result<TResult>> {\n let res: Response | null = null;\n let err: Error | null = null;\n for (let i = 0; i <= this.retry.attempts; i++) {\n const url = new URL(`${this.baseUrl}/${req.path.join(\"/\")}`);\n if (req.query) {\n for (const [k, v] of Object.entries(req.query)) {\n url.searchParams.set(k, v.toString());\n }\n }\n res = await fetch(url, {\n method: req.method,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.rootKey}`,\n \"Unkey-SDK\": this.sdkVersions.join(\",\"),\n },\n cache: this.cache,\n body: JSON.stringify(req.body),\n }).catch((e: Error) => {\n err = e;\n return null; // set `res` to `null`\n });\n if (res?.ok) {\n return { result: (await res.json()) as TResult };\n }\n const backoff = this.retry.backoff(i);\n console.debug(\n \"attempt %d of %d to reach %s failed, retrying in %d ms: %s\",\n i + 1,\n this.retry.attempts + 1,\n url,\n backoff,\n // @ts-ignore I don't understand why `err` is `never`\n err?.message,\n );\n await new Promise((r) => setTimeout(r, backoff));\n }\n\n if (res) {\n return { error: (await res.json()) as ErrorResponse[\"error\"] };\n }\n\n return {\n error: {\n // @ts-ignore\n code: \"FETCH_ERROR\",\n // @ts-ignore I don't understand why `err` is `never`\n message: err?.message ?? \"No response\",\n docs: \"https://developer.mozilla.org/en-US/docs/Web/API/fetch\",\n requestId: \"N/A\",\n },\n };\n }\n\n public get keys() {\n return {\n create: async (\n req: paths[\"/v1/keys.createKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.createKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.createKey\"],\n method: \"POST\",\n body: req,\n });\n },\n update: async (\n req: paths[\"/v1/keys.updateKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.updateKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.updateKey\"],\n method: \"POST\",\n body: req,\n });\n },\n verify: async (\n req: paths[\"/v1/keys.verifyKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.verifyKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.verifyKey\"],\n method: \"POST\",\n body: req,\n });\n },\n delete: async (\n req: paths[\"/v1/keys.deleteKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.deleteKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.deleteKey\"],\n method: \"POST\",\n body: req,\n });\n },\n updateRemaining: async (\n req: paths[\"/v1/keys.updateRemaining\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.updateRemaining\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.updateRemaining\"],\n method: \"POST\",\n body: req,\n });\n },\n get: async (\n req: paths[\"/v1/keys.getKey\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/keys.getKey\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.getKey\"],\n method: \"GET\",\n query: req,\n });\n },\n getVerifications: async (\n req: paths[\"/v1/keys.getVerifications\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.getVerifications\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.getVerifications\"],\n method: \"GET\",\n query: req,\n });\n },\n };\n }\n\n public get apis() {\n return {\n create: async (\n req: paths[\"/v1/apis.createApi\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/apis.createApi\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.createApi\"],\n method: \"POST\",\n body: req,\n });\n },\n delete: async (\n req: paths[\"/v1/apis.deleteApi\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/apis.deleteApi\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.deleteApi\"],\n method: \"POST\",\n body: req,\n });\n },\n get: async (\n req: paths[\"/v1/apis.getApi\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/apis.getApi\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.getApi\"],\n method: \"GET\",\n query: req,\n });\n },\n listKeys: async (\n req: paths[\"/v1/apis.listKeys\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/apis.listKeys\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.listKeys\"],\n method: \"GET\",\n query: req,\n });\n },\n };\n }\n}\n","import { Unkey } from \"./client\";\n\n/**\n * Verify a key\n *\n * @example\n * ```ts\n * const { result, error } = await verifyKey(\"key_123\")\n * if (error){\n * // handle potential network or bad request error\n * // a link to our docs will be in the `error.docs` field\n * console.error(error.message)\n * return\n * }\n * if (!result.valid) {\n * // do not grant access\n * return\n * }\n *\n * // process request\n * console.log(result)\n * ```\n */\nexport function verifyKey(req: string | { key: string; apiId: string }) {\n // yes this is empty to make typescript happy but we don't need a token for verifying keys\n // it's not the cleanest but it works for now :)\n const unkey = new Unkey({ rootKey: \"public\" });\n return unkey.keys.verify(typeof req === \"string\" ? { key: req } : req);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEE,cAAW;;;ACuFN,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACC;AAAA,EACA;AAAA,EACA,cAA8B,CAAC;AAAA,EAEhC;AAAA,EAKhB,YAAY,MAAoB;AAC9B,SAAK,UAAU,KAAK,WAAW;AAC/B,SAAK,UAAU,KAAK,WAAW,KAAK;AACpC,SAAK,YAAY,KAAK,IAAI,OAAO,EAAE;AACnC,QAAI,KAAK,mBAAmB;AAC1B,WAAK,YAAY,KAAK,KAAK,iBAAiB;AAAA,IAC9C;AAEA,SAAK,QAAQ,KAAK;AAIlB,QAAI,CAAC,KAAK,SAAS;AACjB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,SAAK,QAAQ;AAAA,MACX,UAAU,KAAK,OAAO,YAAY;AAAA,MAClC,SAAS,KAAK,OAAO,YAAY,CAAC,MAAM,KAAK,MAAM,KAAK,IAAI,CAAC,IAAI,EAAE;AAAA,IACrE;AAAA,EACF;AAAA,EAEA,MAAc,MAAe,KAA2C;AACtE,QAAI,MAAuB;AAC3B,QAAI,MAAoB;AACxB,aAAS,IAAI,GAAG,KAAK,KAAK,MAAM,UAAU,KAAK;AAC7C,YAAM,MAAM,IAAI,IAAI,GAAG,KAAK,OAAO,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC,EAAE;AAC3D,UAAI,IAAI,OAAO;AACb,mBAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,IAAI,KAAK,GAAG;AAC9C,cAAI,aAAa,IAAI,GAAG,EAAE,SAAS,CAAC;AAA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KAAK,OACH,QAGG;AACH,eAAO,MAAM,KAAK,MAAM;AAAA,UACtB,MAAM,CAAC,MAAM,aAAa;AAAA,UAC1B,QAAQ;AAAA,UACR,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,MACA,UAAU,OACR,QAGG;AACH,eAAO,MAAM,KAAK,MAAM;AAAA,UACtB,MAAM,CAAC,MAAM,eAAe;AAAA,UAC5B,QAAQ;AAAA,UACR,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;;;AC9SO,SAAS,UAAU,KAA8C;AAGtE,QAAM,QAAQ,IAAI,MAAM,EAAE,SAAS,SAAS,CAAC;AAC7C,SAAO,MAAM,KAAK,OAAO,OAAO,QAAQ,WAAW,EAAE,KAAK,IAAI,IAAI,GAAG;AACvE;","names":[]}
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // package.json
2
- var version = "0.13.1";
2
+ var version = "0.15.0";
3
3
 
4
4
  // src/client.ts
5
5
  var Unkey = class {
@@ -121,6 +121,13 @@ var Unkey = class {
121
121
  method: "GET",
122
122
  query: req
123
123
  });
124
+ },
125
+ getVerifications: async (req) => {
126
+ return await this.fetch({
127
+ path: ["v1", "keys.getVerifications"],
128
+ method: "GET",
129
+ query: req
130
+ });
124
131
  }
125
132
  };
126
133
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../package.json","../src/client.ts","../src/verify.ts"],"sourcesContent":["{\n \"name\": \"@unkey/api\",\n \"version\": \"0.13.1\",\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"license\": \"MIT\",\n \"private\": false,\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"keywords\": [\n \"unkey\",\n \"client\",\n \"api\"\n ],\n \"bugs\": {\n \"url\": \"https://github.com/unkeyed/unkey/issues\"\n },\n \"homepage\": \"https://github.com/unkeyed/unkey#readme\",\n \"files\": [\n \"./dist/**\"\n ],\n \"author\": \"Andreas Thomas <andreas@chronark.com>\",\n \"scripts\": {\n \"test\": \"bun test\",\n \"test:coverage\": \"bun test --coverage\",\n \"generate\": \"openapi-typescript https://api.unkey.dev/openapi.json -o ./src/openapi.d.ts\",\n \"build\": \"pnpm generate && tsup\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^20.8.7\",\n \"openapi-typescript\": \"^6.7.2\",\n \"tsup\": \"^7.2.0\",\n \"tsx\": \"^3.14.0\",\n \"typescript\": \"^5.2.2\"\n }\n}\n","import { version } from \"../package.json\";\nimport { ErrorResponse } from \"./errors\";\nimport type { paths } from \"./openapi\";\nexport type UnkeyOptions = (\n | {\n token?: never;\n\n /**\n * The root key from unkey.dev.\n *\n * You can create/manage your root keys here:\n * https://unkey.dev/app/settings/root-keys\n */\n rootKey: string;\n }\n | {\n /**\n * The workspace key from unkey.dev\n *\n * @deprecated Use `rootKey`\n */\n token: string;\n rootKey?: never;\n }\n) & {\n /**\n * @default https://api.unkey.dev\n */\n baseUrl?: string;\n\n /**\n * Retry on network errors\n */\n retry?: {\n /**\n * How many attempts should be made\n * The maximum number of requests will be `attempts + 1`\n * `0` means no retries\n *\n * @default 5\n */\n attempts?: number;\n /**\n * Return how many milliseconds to wait until the next attempt is made\n *\n * @default `(retryCount) => Math.round(Math.exp(retryCount) * 10)),`\n */\n backoff?: (retryCount: number) => number;\n };\n /**\n * Customize the `fetch` cache behaviour\n */\n cache?: RequestCache;\n\n /**\n * The version of the SDK instantiating this client.\n *\n * This is used for internal metrics and is not covered by semver, and may change at any time.\n *\n * You can leave this blank unless you are building a wrapper around this SDK.\n */\n wrapperSdkVersion?: `v${string}`;\n};\n\ntype ApiRequest = {\n path: string[];\n} & (\n | {\n method: \"GET\";\n body?: never;\n query?: Record<string, string | number | boolean>;\n }\n | {\n method: \"POST\";\n body?: unknown;\n query?: never;\n }\n);\n\ntype Result<R> =\n | {\n result: R;\n error?: never;\n }\n | {\n result?: never;\n error: ErrorResponse[\"error\"];\n };\n\nexport class Unkey {\n public readonly baseUrl: string;\n private readonly rootKey: string;\n private readonly cache?: RequestCache;\n private readonly sdkVersions: `v${string}`[] = [];\n\n public readonly retry: {\n attempts: number;\n backoff: (retryCount: number) => number;\n };\n\n constructor(opts: UnkeyOptions) {\n this.baseUrl = opts.baseUrl ?? \"https://api.unkey.dev\";\n this.rootKey = opts.rootKey ?? opts.token;\n this.sdkVersions.push(`v${version}`);\n if (opts.wrapperSdkVersion) {\n this.sdkVersions.push(opts.wrapperSdkVersion);\n }\n\n this.cache = opts.cache;\n /**\n * Even though typescript should prevent this, some people still pass undefined or empty strings\n */\n if (!this.rootKey) {\n throw new Error(\n \"Unkey root key must be set, maybe you passed in `undefined` or an empty string?\",\n );\n }\n\n this.retry = {\n attempts: opts.retry?.attempts ?? 5,\n backoff: opts.retry?.backoff ?? ((n) => Math.round(Math.exp(n) * 10)),\n };\n }\n\n private async fetch<TResult>(req: ApiRequest): Promise<Result<TResult>> {\n let res: Response | null = null;\n let err: Error | null = null;\n for (let i = 0; i <= this.retry.attempts; i++) {\n const url = new URL(`${this.baseUrl}/${req.path.join(\"/\")}`);\n if (req.query) {\n for (const [k, v] of Object.entries(req.query)) {\n url.searchParams.set(k, v.toString());\n }\n }\n res = await fetch(url, {\n method: req.method,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.rootKey}`,\n \"Unkey-SDK\": this.sdkVersions.join(\",\"),\n },\n cache: this.cache,\n body: JSON.stringify(req.body),\n }).catch((e: Error) => {\n err = e;\n return null; // set `res` to `null`\n });\n if (res?.ok) {\n return { result: (await res.json()) as TResult };\n }\n const backoff = this.retry.backoff(i);\n console.debug(\n \"attempt %d of %d to reach %s failed, retrying in %d ms: %s\",\n i + 1,\n this.retry.attempts + 1,\n url,\n backoff,\n // @ts-ignore I don't understand why `err` is `never`\n err?.message,\n );\n await new Promise((r) => setTimeout(r, backoff));\n }\n\n if (res) {\n return { error: (await res.json()) as ErrorResponse[\"error\"] };\n }\n\n return {\n error: {\n // @ts-ignore\n code: \"FETCH_ERROR\",\n // @ts-ignore I don't understand why `err` is `never`\n message: err?.message ?? \"No response\",\n docs: \"https://developer.mozilla.org/en-US/docs/Web/API/fetch\",\n requestId: \"N/A\",\n },\n };\n }\n\n public get keys() {\n return {\n create: async (\n req: paths[\"/v1/keys.createKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.createKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.createKey\"],\n method: \"POST\",\n body: req,\n });\n },\n update: async (\n req: paths[\"/v1/keys.updateKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.updateKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.updateKey\"],\n method: \"POST\",\n body: req,\n });\n },\n verify: async (\n req: paths[\"/v1/keys.verifyKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.verifyKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.verifyKey\"],\n method: \"POST\",\n body: req,\n });\n },\n delete: async (\n req: paths[\"/v1/keys.deleteKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.deleteKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.deleteKey\"],\n method: \"POST\",\n body: req,\n });\n },\n updateRemaining: async (\n req: paths[\"/v1/keys.updateRemaining\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.updateRemaining\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.updateRemaining\"],\n method: \"POST\",\n body: req,\n });\n },\n get: async (\n req: paths[\"/v1/keys.getKey\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/keys.getKey\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.getKey\"],\n method: \"GET\",\n query: req,\n });\n },\n };\n }\n\n public get apis() {\n return {\n create: async (\n req: paths[\"/v1/apis.createApi\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/apis.createApi\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.createApi\"],\n method: \"POST\",\n body: req,\n });\n },\n delete: async (\n req: paths[\"/v1/apis.deleteApi\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/apis.deleteApi\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.deleteApi\"],\n method: \"POST\",\n body: req,\n });\n },\n get: async (\n req: paths[\"/v1/apis.getApi\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/apis.getApi\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.getApi\"],\n method: \"GET\",\n query: req,\n });\n },\n listKeys: async (\n req: paths[\"/v1/apis.listKeys\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/apis.listKeys\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.listKeys\"],\n method: \"GET\",\n query: req,\n });\n },\n };\n }\n}\n","import { Unkey } from \"./client\";\n\n/**\n * Verify a key\n *\n * @example\n * ```ts\n * const { result, error } = await verifyKey(\"key_123\")\n * if (error){\n * // handle potential network or bad request error\n * // a link to our docs will be in the `error.docs` field\n * console.error(error.message)\n * return\n * }\n * if (!result.valid) {\n * // do not grant access\n * return\n * }\n *\n * // process request\n * console.log(result)\n * ```\n */\nexport function verifyKey(req: string | { key: string; apiId: string }) {\n // yes this is empty to make typescript happy but we don't need a token for verifying keys\n // it's not the cleanest but it works for now :)\n const unkey = new Unkey({ rootKey: \"public\" });\n return unkey.keys.verify(typeof req === \"string\" ? { key: req } : req);\n}\n"],"mappings":";AAEE,cAAW;;;ACuFN,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACC;AAAA,EACA;AAAA,EACA,cAA8B,CAAC;AAAA,EAEhC;AAAA,EAKhB,YAAY,MAAoB;AAC9B,SAAK,UAAU,KAAK,WAAW;AAC/B,SAAK,UAAU,KAAK,WAAW,KAAK;AACpC,SAAK,YAAY,KAAK,IAAI,OAAO,EAAE;AACnC,QAAI,KAAK,mBAAmB;AAC1B,WAAK,YAAY,KAAK,KAAK,iBAAiB;AAAA,IAC9C;AAEA,SAAK,QAAQ,KAAK;AAIlB,QAAI,CAAC,KAAK,SAAS;AACjB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,SAAK,QAAQ;AAAA,MACX,UAAU,KAAK,OAAO,YAAY;AAAA,MAClC,SAAS,KAAK,OAAO,YAAY,CAAC,MAAM,KAAK,MAAM,KAAK,IAAI,CAAC,IAAI,EAAE;AAAA,IACrE;AAAA,EACF;AAAA,EAEA,MAAc,MAAe,KAA2C;AACtE,QAAI,MAAuB;AAC3B,QAAI,MAAoB;AACxB,aAAS,IAAI,GAAG,KAAK,KAAK,MAAM,UAAU,KAAK;AAC7C,YAAM,MAAM,IAAI,IAAI,GAAG,KAAK,OAAO,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC,EAAE;AAC3D,UAAI,IAAI,OAAO;AACb,mBAAW,CAAC,GAAG,CAAC,KAAK,OAA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AAAA,UAC1B,QAAQ;AAAA,UACR,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,MACA,UAAU,OACR,QAGG;AACH,eAAO,MAAM,KAAK,MAAM;AAAA,UACtB,MAAM,CAAC,MAAM,eAAe;AAAA,UAC5B,QAAQ;AAAA,UACR,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;;;ACjSO,SAAS,UAAU,KAA8C;AAGtE,QAAM,QAAQ,IAAI,MAAM,EAAE,SAAS,SAAS,CAAC;AAC7C,SAAO,MAAM,KAAK,OAAO,OAAO,QAAQ,WAAW,EAAE,KAAK,IAAI,IAAI,GAAG;AACvE;","names":[]}
1
+ {"version":3,"sources":["../package.json","../src/client.ts","../src/verify.ts"],"sourcesContent":["{\n \"name\": \"@unkey/api\",\n \"version\": \"0.15.0\",\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"license\": \"MIT\",\n \"private\": false,\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"keywords\": [\n \"unkey\",\n \"client\",\n \"api\"\n ],\n \"bugs\": {\n \"url\": \"https://github.com/unkeyed/unkey/issues\"\n },\n \"homepage\": \"https://github.com/unkeyed/unkey#readme\",\n \"files\": [\n \"./dist/**\"\n ],\n \"author\": \"Andreas Thomas <andreas@chronark.com>\",\n \"scripts\": {\n \"test\": \"bun test\",\n \"test:coverage\": \"bun test --coverage\",\n \"generate\": \"openapi-typescript https://api.unkey.dev/openapi.json -o ./src/openapi.d.ts\",\n \"build\": \"pnpm generate && tsup\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^20.8.7\",\n \"@unkey/tsconfig\": \"workspace:^\",\n \"openapi-typescript\": \"^6.7.2\",\n \"tsup\": \"^7.2.0\",\n \"tsx\": \"^3.14.0\",\n \"typescript\": \"^5.2.2\"\n }\n}\n","import { version } from \"../package.json\";\nimport { ErrorResponse } from \"./errors\";\nimport type { paths } from \"./openapi\";\nexport type UnkeyOptions = (\n | {\n token?: never;\n\n /**\n * The root key from unkey.dev.\n *\n * You can create/manage your root keys here:\n * https://unkey.dev/app/settings/root-keys\n */\n rootKey: string;\n }\n | {\n /**\n * The workspace key from unkey.dev\n *\n * @deprecated Use `rootKey`\n */\n token: string;\n rootKey?: never;\n }\n) & {\n /**\n * @default https://api.unkey.dev\n */\n baseUrl?: string;\n\n /**\n * Retry on network errors\n */\n retry?: {\n /**\n * How many attempts should be made\n * The maximum number of requests will be `attempts + 1`\n * `0` means no retries\n *\n * @default 5\n */\n attempts?: number;\n /**\n * Return how many milliseconds to wait until the next attempt is made\n *\n * @default `(retryCount) => Math.round(Math.exp(retryCount) * 10)),`\n */\n backoff?: (retryCount: number) => number;\n };\n /**\n * Customize the `fetch` cache behaviour\n */\n cache?: RequestCache;\n\n /**\n * The version of the SDK instantiating this client.\n *\n * This is used for internal metrics and is not covered by semver, and may change at any time.\n *\n * You can leave this blank unless you are building a wrapper around this SDK.\n */\n wrapperSdkVersion?: `v${string}`;\n};\n\ntype ApiRequest = {\n path: string[];\n} & (\n | {\n method: \"GET\";\n body?: never;\n query?: Record<string, string | number | boolean>;\n }\n | {\n method: \"POST\";\n body?: unknown;\n query?: never;\n }\n);\n\ntype Result<R> =\n | {\n result: R;\n error?: never;\n }\n | {\n result?: never;\n error: ErrorResponse[\"error\"];\n };\n\nexport class Unkey {\n public readonly baseUrl: string;\n private readonly rootKey: string;\n private readonly cache?: RequestCache;\n private readonly sdkVersions: `v${string}`[] = [];\n\n public readonly retry: {\n attempts: number;\n backoff: (retryCount: number) => number;\n };\n\n constructor(opts: UnkeyOptions) {\n this.baseUrl = opts.baseUrl ?? \"https://api.unkey.dev\";\n this.rootKey = opts.rootKey ?? opts.token;\n this.sdkVersions.push(`v${version}`);\n if (opts.wrapperSdkVersion) {\n this.sdkVersions.push(opts.wrapperSdkVersion);\n }\n\n this.cache = opts.cache;\n /**\n * Even though typescript should prevent this, some people still pass undefined or empty strings\n */\n if (!this.rootKey) {\n throw new Error(\n \"Unkey root key must be set, maybe you passed in `undefined` or an empty string?\",\n );\n }\n\n this.retry = {\n attempts: opts.retry?.attempts ?? 5,\n backoff: opts.retry?.backoff ?? ((n) => Math.round(Math.exp(n) * 10)),\n };\n }\n\n private async fetch<TResult>(req: ApiRequest): Promise<Result<TResult>> {\n let res: Response | null = null;\n let err: Error | null = null;\n for (let i = 0; i <= this.retry.attempts; i++) {\n const url = new URL(`${this.baseUrl}/${req.path.join(\"/\")}`);\n if (req.query) {\n for (const [k, v] of Object.entries(req.query)) {\n url.searchParams.set(k, v.toString());\n }\n }\n res = await fetch(url, {\n method: req.method,\n headers: {\n \"Content-Type\": \"application/json\",\n Authorization: `Bearer ${this.rootKey}`,\n \"Unkey-SDK\": this.sdkVersions.join(\",\"),\n },\n cache: this.cache,\n body: JSON.stringify(req.body),\n }).catch((e: Error) => {\n err = e;\n return null; // set `res` to `null`\n });\n if (res?.ok) {\n return { result: (await res.json()) as TResult };\n }\n const backoff = this.retry.backoff(i);\n console.debug(\n \"attempt %d of %d to reach %s failed, retrying in %d ms: %s\",\n i + 1,\n this.retry.attempts + 1,\n url,\n backoff,\n // @ts-ignore I don't understand why `err` is `never`\n err?.message,\n );\n await new Promise((r) => setTimeout(r, backoff));\n }\n\n if (res) {\n return { error: (await res.json()) as ErrorResponse[\"error\"] };\n }\n\n return {\n error: {\n // @ts-ignore\n code: \"FETCH_ERROR\",\n // @ts-ignore I don't understand why `err` is `never`\n message: err?.message ?? \"No response\",\n docs: \"https://developer.mozilla.org/en-US/docs/Web/API/fetch\",\n requestId: \"N/A\",\n },\n };\n }\n\n public get keys() {\n return {\n create: async (\n req: paths[\"/v1/keys.createKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.createKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.createKey\"],\n method: \"POST\",\n body: req,\n });\n },\n update: async (\n req: paths[\"/v1/keys.updateKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.updateKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.updateKey\"],\n method: \"POST\",\n body: req,\n });\n },\n verify: async (\n req: paths[\"/v1/keys.verifyKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.verifyKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.verifyKey\"],\n method: \"POST\",\n body: req,\n });\n },\n delete: async (\n req: paths[\"/v1/keys.deleteKey\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.deleteKey\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.deleteKey\"],\n method: \"POST\",\n body: req,\n });\n },\n updateRemaining: async (\n req: paths[\"/v1/keys.updateRemaining\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.updateRemaining\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.updateRemaining\"],\n method: \"POST\",\n body: req,\n });\n },\n get: async (\n req: paths[\"/v1/keys.getKey\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/keys.getKey\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.getKey\"],\n method: \"GET\",\n query: req,\n });\n },\n getVerifications: async (\n req: paths[\"/v1/keys.getVerifications\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<\n paths[\"/v1/keys.getVerifications\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"keys.getVerifications\"],\n method: \"GET\",\n query: req,\n });\n },\n };\n }\n\n public get apis() {\n return {\n create: async (\n req: paths[\"/v1/apis.createApi\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/apis.createApi\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.createApi\"],\n method: \"POST\",\n body: req,\n });\n },\n delete: async (\n req: paths[\"/v1/apis.deleteApi\"][\"post\"][\"requestBody\"][\"content\"][\"application/json\"],\n ): Promise<\n Result<\n paths[\"/v1/apis.deleteApi\"][\"post\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]\n >\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.deleteApi\"],\n method: \"POST\",\n body: req,\n });\n },\n get: async (\n req: paths[\"/v1/apis.getApi\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/apis.getApi\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.getApi\"],\n method: \"GET\",\n query: req,\n });\n },\n listKeys: async (\n req: paths[\"/v1/apis.listKeys\"][\"get\"][\"parameters\"][\"query\"],\n ): Promise<\n Result<paths[\"/v1/apis.listKeys\"][\"get\"][\"responses\"][\"200\"][\"content\"][\"application/json\"]>\n > => {\n return await this.fetch({\n path: [\"v1\", \"apis.listKeys\"],\n method: \"GET\",\n query: req,\n });\n },\n };\n }\n}\n","import { Unkey } from \"./client\";\n\n/**\n * Verify a key\n *\n * @example\n * ```ts\n * const { result, error } = await verifyKey(\"key_123\")\n * if (error){\n * // handle potential network or bad request error\n * // a link to our docs will be in the `error.docs` field\n * console.error(error.message)\n * return\n * }\n * if (!result.valid) {\n * // do not grant access\n * return\n * }\n *\n * // process request\n * console.log(result)\n * ```\n */\nexport function verifyKey(req: string | { key: string; apiId: string }) {\n // yes this is empty to make typescript happy but we don't need a token for verifying keys\n // it's not the cleanest but it works for now :)\n const unkey = new Unkey({ rootKey: \"public\" });\n return unkey.keys.verify(typeof req === \"string\" ? { key: req } : req);\n}\n"],"mappings":";AAEE,cAAW;;;ACuFN,IAAM,QAAN,MAAY;AAAA,EACD;AAAA,EACC;AAAA,EACA;AAAA,EACA,cAA8B,CAAC;AAAA,EAEhC;AAAA,EAKhB,YAAY,MAAoB;AAC9B,SAAK,UAAU,KAAK,WAAW;AAC/B,SAAK,UAAU,KAAK,WAAW,KAAK;AACpC,SAAK,YAAY,KAAK,IAAI,OAAO,EAAE;AACnC,QAAI,KAAK,mBAAmB;AAC1B,WAAK,YAAY,KAAK,KAAK,iBAAiB;AAAA,IAC9C;AAEA,SAAK,QAAQ,KAAK;AAIlB,QAAI,CAAC,KAAK,SAAS;AACjB,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAEA,SAAK,QAAQ;AAAA,MACX,UAAU,KAAK,OAAO,YAAY;AAAA,MAClC,SAAS,KAAK,OAAO,YAAY,CAAC,MAAM,KAAK,MAAM,KAAK,IAAI,CAAC,IAAI,EAAE;AAAA,IACrE;AAAA,EACF;AAAA,EAEA,MAAc,MAAe,KAA2C;AACtE,QAAI,MAAuB;AAC3B,QAAI,MAAoB;AACxB,aAAS,IAAI,GAAG,KAAK,KAAK,MAAM,UAAU,KAAK;AAC7C,YAAM,MAAM,IAAI,IAAI,GAAG,KAAK,OAAO,IAAI,IAAI,KAAK,KAAK,GAAG,CAAC,EAAE;AAC3D,UAAI,IAAI,OAAO;AACb,mBAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,IAAI,KAAK,GAAG;AAC9C,cAAI,aAAa,IAAI,GAAG,EAAE,SAAS,CAAC;AAAA,QACtC;AAAA,MACF;AACA,YAAM,MAAM,MAAM,KAAK;AAAA,QACrB,QAAQ,IAAI;AA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aAAa;AAAA,UAC1B,QAAQ;AAAA,UACR,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,MACA,UAAU,OACR,QAGG;AACH,eAAO,MAAM,KAAK,MAAM;AAAA,UACtB,MAAM,CAAC,MAAM,eAAe;AAAA,UAC5B,QAAQ;AAAA,UACR,OAAO;AAAA,QACT,CAAC;AAAA,MACH;AAAA,IACF;AAAA,EACF;AACF;;;AC9SO,SAAS,UAAU,KAA8C;AAGtE,QAAM,QAAQ,IAAI,MAAM,EAAE,SAAS,SAAS,CAAC;AAC7C,SAAO,MAAM,KAAK,OAAO,OAAO,QAAQ,WAAW,EAAE,KAAK,IAAI,IAAI,GAAG;AACvE;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unkey/api",
3
- "version": "0.13.1",
3
+ "version": "0.15.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "license": "MIT",
@@ -26,7 +26,8 @@
26
26
  "openapi-typescript": "^6.7.2",
27
27
  "tsup": "^7.2.0",
28
28
  "tsx": "^3.14.0",
29
- "typescript": "^5.2.2"
29
+ "typescript": "^5.2.2",
30
+ "@unkey/tsconfig": "^0.0.0"
30
31
  },
31
32
  "scripts": {
32
33
  "test": "bun test",